Search code examples
javaformattingformatlocalenumber-formatting

Java format double value with german locale


I have the double value 1400.0 and now I want to format this value into 1.400,00 in Java.

currently I do:

double doubleValue = 1400.0    
String pattern = "0.00";
DecimalFormat format = (DecimalFormat)NumberFormat.getNumberInstance(Locale.GERMAN)
format .applyPattern(pattern);
// will be 1400,00 and not 1.400,00
String formattedValue = format.format(doubleValue);

What am I missing?


Solution

  • Try:

    String pattern = "#,##0.00";
    

    This pattern tells a DecimalFormat to add the grouping character to the given number.

    Edit: Originally I wrote this with zeros, but this would actually create extra zeros for small numbers. Use the # pattern where you don't want initial zeros.