本文介绍了没有为DecimalFormat分组的分隔符char的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

DecimalFormat 使用','作为默认分组分隔符.

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

告诉DecimalFormat我们不需要任何分组分隔符的正确方法是什么?我目前使用的是symbols.setGroupingSeparator('\ 0'),它似乎可以正常工作,但是看起来很丑.

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);

告诉DecimalFormat我们不需要任何分组分隔符char的正确方法是什么?

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

推荐答案

通过调用:

df.setGroupingUsed(false);

这篇关于没有为DecimalFormat分组的分隔符char的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-17 20:23