I'm trying to format some numbers in a Java program. The numbers will be both doubles and integers. When handling doubles, I want to keep only two decimal points but when handling integers I want the program to keep them unaffected. In other words:
Doubles - Input
14.0184849945
Doubles - Output
14.01
Integers - Input
13
Integers - Output
13 (not 13.00)
Is there a way to implement this in the same DecimalFormat instance? My code is the following, so far:
DecimalFormat df = new DecimalFormat("#,###,##0.00");
DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
otherSymbols.setDecimalSeparator('.');
otherSymbols.setGroupingSeparator(',');
df.setDecimalFormatSymbols(otherSymbols);
You can just set the
minimumFractionDigits
to 0. Like this:Could you not just wrapper this into a Utility call. For example
You can then just do things like:
MyFormatter.format(int)
etc.