Java NumberFormat ignores comma in US locale?

I don’t understand something in NumberFormat, in US locale it is supposed to consider a comma (",") as a separator of groups - for thousands.

Why does he ignore commas in the wrong places for this locale?

NumberFormat format = DecimalFormat.getInstance(Locale.US); 
System.out.println(format.parse("5,500").longValue()); //5500
System.out.println(format.parse("550,0").longValue()); //5500
System.out.println(format.parse("5500,").longValue()); //5500

Any other ideas on how to parse String to Long according to the locale (aluminize this input ",", in the wrong places for the locale should not be)?

+5
source share
3 answers

The comma is the placeholder for the grouping separator, and the period is the placeholder for the decimal separator.

NumberFormat format = DecimalFormat.getInstance(Locale.US); 
System.out.println(format.parse("5.500").longValue()); //5
System.out.println(format.parse("55.00").longValue()); //55
System.out.println(format.parse("550.0").longValue()); //550

. , , , . , . - 5,5.0,0,, .

, 3 2 .

, , . , .


java.text.DecimalFormat, Oracle, , .

1526  // Ignore grouping characters, if we are using them, but
1527  // require that they be followed by a digit.  Otherwise
1528  // we backup and reprocess them.

...

0

, . , parse ( NumberFormat/DecimalFormat), .

0

Since the comma is a delimiter for thousands in the US Locale, it is ignored if misplaced. All this weekend will print 5500.

-1
source

All Articles