Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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(''), and it seems to work, but it looks ugly.

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
606 views
Welcome To Ask or Share your Answers For Others

1 Answer

Turn off grouping by calling setGroupingUsed:

df.setGroupingUsed(false);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...