damians
damians

Reputation: 13

Java DecimalFormat - incorrect number format in linux

I have a problem with parsing decimal number in Linux environment. When I parse in Windows, everything's all right.

Below, code snippet

String price; DecimalFormat FORMATTER = (DecimalFormat)DecimalFormat.getInstance(); double customPrice = FORMATTER.parse(price).doubleValue();

And results

When price='9' Then customPrice='9.0' - it is ok

When price='1,00' Then customPrice='100.0' - it is wrong

When price='25,00' Then customPrice='2500.0' - it is wrong

Can you tell me what the problem is ? Thanks

Upvotes: 0

Views: 592

Answers (2)

JavaBohne
JavaBohne

Reputation: 181

You can change the decimal separator of DecimalFormat. The easiest way by using a NumberFormat with the desired locale, e.g.:

NumberFormat FORMATTER = NumberFormat.getNumberInstance(Locale.GERMAN);
double customPrice = FORMATTER.parse(price).doubleValue();

Upvotes: 1

GuyKhmel
GuyKhmel

Reputation: 505

Read here here about locales, probably you're using en_US which causes the ',' symbol to separate groups of thousands.

you can use also

DecimalFormatSymbols unusualSymbols = new DecimalFormatSymbols(currentLocale);
unusualSymbols.setDecimalSeparator(',');
DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);
weirdFormatter.setGroupingSize(4);

to state your own sepeartors

Upvotes: 1

Related Questions