Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Use DecimalFormat to format doubles and integers but keep integers without a decimal separator

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);
like image 391
Lefteris008 Avatar asked Apr 30 '13 21:04

Lefteris008


People also ask

What is the use of DecimalFormat in Java?

You can use the DecimalFormat class to format decimal numbers into locale-specific strings. This class allows you to control the display of leading and trailing zeros, prefixes and suffixes, grouping (thousands) separators, and the decimal separator.

How do you change the decimal separator in Java?

Use String.format("%. 2f", d); This method uses our JVM's default Locale to choose the decimal separator.

Is Java DecimalFormat thread safe?

DecimalFormat is not thread safe. See: docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html "Decimal formats are generally not synchronized. It is recommended to create separate format instances for each thread. If multiple threads access a format concurrently, it must be synchronized externally."


2 Answers

You can just set the minimumFractionDigits to 0. Like this:

public class Test {

    public static void main(String[] args) {
        System.out.println(format(14.0184849945)); // prints '14.01'
        System.out.println(format(13)); // prints '13'
        System.out.println(format(3.5)); // prints '3.5'
        System.out.println(format(3.138136)); // prints '3.13'
    }

    public static String format(Number n) {
        NumberFormat format = DecimalFormat.getInstance();
        format.setRoundingMode(RoundingMode.FLOOR);
        format.setMinimumFractionDigits(0);
        format.setMaximumFractionDigits(2);
        return format.format(n);
    }

}
like image 138
Rodrigo Sasaki Avatar answered Oct 10 '22 20:10

Rodrigo Sasaki


Could you not just wrapper this into a Utility call. For example

public class MyFormatter {

  private static DecimalFormat df;
  static {
    df = new DecimalFormat("#,###,##0.00");
    DecimalFormatSymbols otherSymbols = new   DecimalFormatSymbols(Locale.ENGLISH);
    otherSymbols.setDecimalSeparator('.');
    otherSymbols.setGroupingSeparator(',');
    df.setDecimalFormatSymbols(otherSymbols);
  }

  public static <T extends Number> String format(T number) {
     if (Integer.isAssignableFrom(number.getClass())
       return number.toString();

     return df.format(number);
  }
}

You can then just do things like: MyFormatter.format(int) etc.

like image 42
Jeremy Unruh Avatar answered Oct 10 '22 21:10

Jeremy Unruh