Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting doubles to two decimal places in Java produces a comma instead of a dot [duplicate]

I have read many threads, and it seems to be the best solution to keep 2 places in my double number:

DecimalFormat df = new DecimalFormat("#.00");
double d = 1.234567;
System.out.println(df.format(d));

But when I use it, it prints:

1,23

I want to keep the DOT, cause I need this format (#.##) to use (I will use it as string). How do I keep this dot?

like image 665
fhbeltrami Avatar asked Nov 30 '22 11:11

fhbeltrami


1 Answers

If you want a dot rather than a comma, you should specify a Locale which uses dot as the decimal separator, e.g.

DecimalFormat df = new DecimalFormat("#.00",
                                    DecimalFormatSymbols.getInstance(Locale.US));

Basically, "." in a format pattern doesn't mean "dot", it means "decimal separator".

like image 187
Jon Skeet Avatar answered Dec 09 '22 11:12

Jon Skeet