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

Categories

0 votes
608 views
in Technique[技术] by (71.8m points)

java - no grouping separator char for DecimalFormat

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

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

1 Answer

0 votes
by (71.8m points)

Turn off grouping by calling setGroupingUsed:

df.setGroupingUsed(false);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...