简体   繁体   English

没有用于DecimalFormat的分组分隔符char

[英]no grouping separator char for DecimalFormat

DecimalFormat uses ',' as the default grouping separator character. DecimalFormat使用','作为默认的分组分隔符。

What is the correct way to tell DecimalFormat that we don't want any grouping separator character? 告诉DecimalFormat我们不想要任何分组分隔符的正确方法是什么? I currently use symbols.setGroupingSeparator('\\0'), and it seems to work, but it looks ugly. 我目前使用symbols.setGroupingSeparator('\\ 0'),它似乎工作,但它看起来很丑。

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? 告诉DecimalFormat我们不想要任何分组分隔符char的正确方法是什么?

通过调用setGroupingUsed关闭分组:

df.setGroupingUsed(false);

You can use the setGroupingUsed() method, but that won't make the format reject the number, it will just stop parsing at the first separator. 您可以使用setGroupingUsed()方法,但这不会使格式拒绝该数字,它将在第一个分隔符处停止解析。 Number parsing with formats will ignore everything after the first invalid character. 使用格式解析数字将忽略第一个无效字符后的所有内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM