[英]How to set space separator to float DecimalFormat?
How to set space as thousand separator for DecimalFormat in float? 如何在float中为DecimalFormat设置空间为千位分隔符? I want to show 13,52 as 13,5, but 13,00 as 13 and I did this with
我想将13,52显示为13,5,但将13,00显示为13并且我这样做了
new DecimalFormat("#.#").format(fIncome)
but I want to 1400,5 be 1 400,5 and 1400 to be 1 400. 但我想1400,5是1 400,5和1400是1 400。
For double I use this code (I don't want to shows numbers after comma in dCount): 对于double我使用此代码(我不想在dCount中显示逗号后的数字):
String.format("%,d", (int)dCount)
But how to use this for floating number with 1 number after comma? 但是如何用逗号后用1号码浮点数?
The accepted answer probably lacks a few lines of code, because it doesn't work as described. 接受的答案可能缺少几行代码,因为它不像描述的那样工作。 Here's how I did it:
我是这样做的:
private static final String DECIMAL_FORMAT = "###,###.#";
private String formatValue(Number value, String formatString) {
DecimalFormatSymbols formatSymbols = new DecimalFormatSymbols(Locale.ENGLISH);
formatSymbols.setDecimalSeparator('.');
formatSymbols.setGroupingSeparator(' ');
DecimalFormat formatter = new DecimalFormat(formatString, formatSymbols);
return formatter.format(value);
}
I then call it like this: 然后我这样称呼它:
formatValue(value, DECIMAL_FORMAT);
To explain how it works, the .
为了解释它是如何工作的,
.
in DECIMAL_FORMAT is replaced with decimalSeparator and the ,
is replaced with groupingSeparator, which is a space in this case. 在DECIMAL_FORMAT替换decimalSeparator和
,
被替换为使用groupingSeparator,这是在该情况下的空间中。
好的,我得到了我想要的东西:)
new DecimalFormat("###,###.#").format(fIncome)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.