Jean Tehhe
Jean Tehhe

Reputation: 1357

Java BigDecimal can have comma instead dot?

I have a string value that I want to assign to a BigDecimal. When I update the decimal value with a number like 100.23, it works fine but when I update it with a number like 100,23 the code throws an exception. Why is that?

Upvotes: 34

Views: 68776

Answers (3)

Kevin Bowersox
Kevin Bowersox

Reputation: 94469

The BigDecimal(String) constructor documentation lists all valid formats and characters. Notice that the , is not included within this list.

Upvotes: 6

Kayz
Kayz

Reputation: 667

If you can't be sure if your String has commas or points, you can use replace(char, char) from the String class. For example myString.replace(',', '.').

Upvotes: 9

No Idea For Name
No Idea For Name

Reputation: 11577

because you tried to put a "," in a number.

you can use this code to parse a number with comma:

NumberFormat.getNumberInstance(Locale.FRANCE).parse("265,858")

you should also use float or double if there is no particular reason you use decimal.

Upvotes: 21

Related Questions