Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 651
MaVRoSCy Avatar asked Feb 05 '23 06:02

MaVRoSCy


2 Answers

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)

like image 107
assylias Avatar answered Feb 17 '23 08:02

assylias


This will be your system locale, different countries usedifferent characters for the decimal and thousand separator.

You can set the locale in the decimal format to override your system default. Or you can change your system default.

like image 36
MartinByers Avatar answered Feb 17 '23 08:02

MartinByers