Reputation: 5671
I do not like the behaviour of Java's DecimalFormat class.
What I'm trying to do is input a value of "0.23" in a text field using the German locale (where '.' is the grouping character, not the decimal !!).
Expected result would be:
with grouping enabled: ParseException, because the grouping character is in the wrong place
with grouping disabled: ParseException, because the '.' character is not allowed.
Actual result:
with grouping enabled: 23.0
with grouping disabled: 0.0
Does anyone know of a better implementation, or should I file this as a Java Bug ?
Upvotes: 1
Views: 548
Reputation: 7546
Locale.setDefault(Locale.GERMAN);
DecimalFormat format = new DecimalFormat();
Number d = format.parse("0.23");
System.out.println(d);
d = format.parse("1.000,23");
System.out.println(d);
This works for me, output is 23 and 1000.23.
Upvotes: 0
Reputation: 8466
When groupping is disabled, the char '.' can not be part of a number.
From API of parse(String source) of NumerFormat:
"The method may not use the entire text of the given string."
So the method will stop parsing after reading the first char which can't be part of the number. So "0.23" is always 0. and "0,1.2.3" is 0,1
Upvotes: 1