textformat java_java.text.DecimalFormat为零时为空吗?

您可能可以使用MessageFormat,特别是其ChoiceFormat功能:

double[] nums = {

-876.123, -0.1, 0, +.5, 100, 123.45678,

};

for (double num : nums) {

System.out.println(

num + " " +

MessageFormat.format(

"{0,choice,-1#negative|0#zero|0

)

);

}

打印:

-876.123 negative

-0.1 negative

0.0 zero

0.5 0.500

100.0 1,00.000

123.45678 1,23.457

请注意,MessageFormat确实在幕后使用了DecimalFormat.从the documentation开始:

FORMAT TYPE: number

FORMAT STYLE: subformatPattern

SUBFORMAT CREATED: new DecimalFormat(

subformatPattern,

DecimalFormatSymbols.getInstance(getLocale())

)

因此,尽管间接地使用了DecimalFormat.如果出于某种原因禁止这样做,则必须自己检查特殊条件,因为DecimalFormat不会区分零.从the documentation:

DecimalFormat patterns have the following syntax:

06003

没有提供零的特殊模式的选项,因此没有DecimalFormat模式可以为您执行此操作.您可以进行if-check检查,也可以只让MessageFormat / ChoiceFormat为您完成检查,如上所示.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值