如何设置空格分隔符以浮动DecimalFormat?

Sai*_*men 4 java android decimal

如何在float中为DecimalFormat设置空间为千位分隔符?我想将13,52显示为13,5,但将13,00显示为13并且我这样做了

new DecimalFormat("#.#").format(fIncome)
Run Code Online (Sandbox Code Playgroud)

但我想1400,5是1 400,5和1400是1 400.

对于double我使用此代码(我不想在dCount中显示逗号后的数字):

String.format("%,d", (int)dCount)
Run Code Online (Sandbox Code Playgroud)

但是如何用逗号后用1号码浮点数?

Joh*_*Eye 10

接受的答案可能缺少几行代码,因为它不像描述的那样工作.我是这样做的:

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);
    }
Run Code Online (Sandbox Code Playgroud)

然后我这样称呼它:

formatValue(value, DECIMAL_FORMAT);
Run Code Online (Sandbox Code Playgroud)

为了解释它的工作原理,将.DECIMAL_FORMAT替换为decimalSeparator,并将其,替换为groupingSeparator,在这种情况下,它是一个空格.


Sai*_*men 3

好吧,我得到了我想要的:)

new DecimalFormat("###,###.#").format(fIncome)
Run Code Online (Sandbox Code Playgroud)