Search code examples
javajava-7decimalformatjdk1.7

DecimalFormat not working properly after windows update


Up until recently my code was working fine on my development machine as well as on the deployment server.

Now out of the blue, the DecimalFormat does not work as expected and I am pretty sure that is after the windows 10 Creators Update.

My code is:

double x = 22.44;
DecimalFormat df = new DecimalFormat("0.00");
System.out.println(df.format(x));

Output: 22,44 Instead of 22.44

If i change it to :

double x = 22.44;
DecimalFormat df = new DecimalFormat("0,00");
System.out.println(df.format(x));

Output is: 0.22

I am using netbeans 7.4 with jdk 1.7.0_79u (64 bit) Tried changing my jdk to 1.7.0_80u (32 bit) but made no difference. Also changed the locale setting for Decimal Symbol and Digit Grouping Symbol but still the same problem.

Anyone with ideas on how to solve this issue?


Solution

  • This is likely a locale issue - your current code uses the default locale of the system, which may be done differently in Java 7 and Java 8. If you want to use a specific locale you can use:

    double x = 22.44;
    DecimalFormat df = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.FRANCE));
    System.out.println(df.format(x));
    
    df = new DecimalFormat("0.00", new DecimalFormatSymbols(Locale.UK));
    System.out.println(df.format(x));
    

    which outputs:

    22,44 (with a comma)
    22.44 (with a dot)