0

I am having trouble parsing "1,234.56" in Java, a similar question recommend using French locale in number formatting, but it is parsing the result incorrectly. Here is what I have:

NumberFormat format = NumberFormat.getInstance(Locale.FRANCE);
Number number = format.parse("1,234.56");
System.out.println(number.doubleValue()); // Should get 1234.56, got 1.234 instead
Number number = format.parse("1,234,567.89");
System.out.println(number.doubleValue());  // Should get 1234567.89
Community
  • 1
  • 1
Bill Software Engineer
  • 6,708
  • 20
  • 82
  • 151

3 Answers3

8

1,234.56 is not French notation. (Something like 1.234,56 is).

Change your locale to Locale.ENGLISH, Locale.US, or similar

Bathsheba
  • 227,678
  • 33
  • 352
  • 470
2

French locale has the decimal point represented by a comma. You'll need to use the US locale:

NumberFormat format = NumberFormat.getInstance(Locale.US);

or Locale.ENGLISH based on the language:

NumberFormat format = NumberFormat.getInstance(Locale.ENGLISH);
M A
  • 69,673
  • 13
  • 131
  • 165
1

You could use string replacement and replace "," for ""

format.parse(("1,234.56").replace(",", ""));

Its messy, but you dont need to solve locale.

Martin Perry
  • 8,795
  • 8
  • 46
  • 106