Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no grouping separator char for DecimalFormat

Tags:

java

DecimalFormat uses ',' as the default grouping separator character.

What is the correct way to tell DecimalFormat that we don't want any grouping separator character? I currently use symbols.setGroupingSeparator('\0'), and it seems to work, but it looks ugly.

DecimalFormatSymbols symbols = new DecimalFormatSymbols();
symbols.setDecimalSeparator('.');
symbols.setGroupingSeparator('\0');

DecimalFormat df = new DecimalFormat();
df.setDecimalFormatSymbols(symbols);

ParsePosition pp = new ParsePosition(0);
Number result = df.parse("44,000.0", pp);   // this should fail, as I don't want any grouping separatator char.
if (pp.getIndex() != input.length())
   throw new Exception("invalid number: " + input, 0);

What is the correct way to tell DecimalFormat that we don't want any grouping separator char?

like image 381
David Portabella Avatar asked Jan 27 '12 15:01

David Portabella


1 Answers

Turn off grouping by calling setGroupingUsed:

df.setGroupingUsed(false);
like image 163
dogbane Avatar answered Sep 16 '22 15:09

dogbane