Bastian Voigt
Bastian Voigt

Reputation: 5671

Why does DecimalFormat ignore grouping characters?

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:

Actual result:

Does anyone know of a better implementation, or should I file this as a Java Bug ?

Upvotes: 1

Views: 548

Answers (2)

Dorus
Dorus

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

SKi
SKi

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

Related Questions