简体   繁体   English

Java十进制格式问题零(0.00)

[英]Java Decimal Format issue zero (0.00)

I have a problem with the formatter, when I give a 0.00 return +0,00 E but I would like to 0,00 E. 我有格式化程序的问题,当我给0.00返回+0,00 E但是我想要0,00 E.

String PATTERN = "###,##0.00\u00a0\u00A4";
DecimalFormatSymbols SYMBOLS = DecimalFormatSymbols.getInstance(Locale.FRANCE);
DecimalFormat FORMATTER_SIGN = new DecimalFormat(PATTERN, SYMBOLS);
FORMATTER_SIGN.setNegativePrefix("-\u00a0");
FORMATTER_SIGN.setPositivePrefix("+\u00a0");
FORMATTER_SIGN.format("0.00") // this

I can get rid of the "+" when I remove the line 我删除该行时可以删除“+”

FORMATTER_SIGN.setPositivePrefix("+\u00a0");

or change it to 或改为

FORMATTER_SIGN.setPositivePrefix("");

Resulting in 导致

0,00 € 0,00€

PS: Your format call doesn't work for me, I need to do something like this: PS:你的format调用对我不起作用,我需要做这样的事情:

FORMATTER_SIGN.format(Double.valueOf("0.00")); // this

You could create your own DecimalFormat that is aware of zeros (simplified example): 您可以创建自己的DecimalFormat,它知道零(简化示例):

class ZeroAwareDecimalFormat extends DecimalFormat {
    private final DecimalFormat zeroFormat;

    public ZeroAwareDecimalFormat(String posNegPattern, String zeroPattern) {
        super(posNegPattern);
        zeroFormat = new DecimalFormat(zeroPattern);
    }

    @Override
    public StringBuffer format(long number, StringBuffer result, FieldPosition fieldPosition) {
        if (number == 0L) {
            return zeroFormat.format(number, result, fieldPosition);
        } else {
            return super.format(number, result, fieldPosition);
        }
    }

    // Override the other methods accordingly.
    // set... methods should be propagated to super and zeroFormat.
}

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

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