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

时间:2015-08-28 10:38:44

标签: java decimalformat

我有格式化程序的问题,当我给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

2 个答案:

答案 0 :(得分:2)

当我删除行

时,我可以删除“+”
FORMATTER_SIGN.setPositivePrefix("+\u00a0");

或将其更改为

FORMATTER_SIGN.setPositivePrefix("");

导致

  

0,00€

PS:你的format电话对我不起作用,我需要这样做:

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

答案 1 :(得分:2)

您可以创建自己的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.
}