0

i need the decimal places output to be like this: 1000.00 -> 1000 1000.10 -> 1000.10 remove trailing zero if decimal places is both zero, but keep two decimal places if there is at least one value.

I tried DecimalFormat(".##"), but its not what im looking for: 1000 1000.1 (need to keep two decimal places 1000.10)

caden
  • 3
  • 1

1 Answers1

0

You can add the following for 2 decimal places or none.

if ((long) x == x) {
   System.out.println((long) x);
} else {
   System.out.printf("%.2f%n", x);
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130