In java, is it possible to use String.format to only show a decimal if there is actually a need? For example, if I do this:
String.format(\"%.1f\", amount);
This might get you what your looking for; I'm not sure of the requirements or context of your request.
float f;
f = 1f
System.out.printf(f==Math.round(f) ? "%d\n" : "%s\n", f); //1
f = 1.555f
System.out.printf(f==Math.round(f) ? "%d\n" : "%s\n", f); //1.555
Worked great for what I needed.
FYI, above, System.out.printf(fmt, x) is like System.out.print(String.format(fmt, x)
No, you have to use DecimalFormat:
final DecimalFormat f = new DecimalFormat("0.##");
System.out.println(f.format(1.3));
System.out.println(f.format(1.0));
Put as many #
s as you'd like; the DecimalFormat will only print as many digits as it thinks are significant, up to the number of #
s.