Java 十进制格式问题零 (0.00)
Java Decimal Format issue zero (0.00)
格式化程序有问题,当我给出 0.00 return +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
当我删除行时我可以去掉“+”
FORMATTER_SIGN.setPositivePrefix("+\u00a0");
或改为
FORMATTER_SIGN.setPositivePrefix("");
导致
0,00 €
PS:你的format
电话对我不起作用,我需要做这样的事情:
FORMATTER_SIGN.format(Double.valueOf("0.00")); // this
您可以创建自己的识别零的 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.
}
格式化程序有问题,当我给出 0.00 return +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
当我删除行时我可以去掉“+”
FORMATTER_SIGN.setPositivePrefix("+\u00a0");
或改为
FORMATTER_SIGN.setPositivePrefix("");
导致
0,00 €
PS:你的format
电话对我不起作用,我需要做这样的事情:
FORMATTER_SIGN.format(Double.valueOf("0.00")); // this
您可以创建自己的识别零的 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.
}