[英]Display only 2 digits after decimal
bill[p][l][0] = new DecimalFormat("##.##").format(Double.parseDouble(i2[m][0]));
The code entered above is not working; 上面输入的代码不起作用; input
10.0
gives 10
as output. 输入
10.0
给出10
作为输出。
It is working - a # means the digit is only printed if it's relevant (see the documentation ). 它正在工作 - #表示仅在相关时打印数字(参见文档 )。 Try using
尝试使用
bill[p][l][0] = new DecimalFormat("##.00").format(Double.parseDouble(i2[m][0]));
要准确格式化最多两个小数位,您应该使用##.00
作为##.##
将从值中删除尾随零。
If you want to handle both positive and negative numbers I would recommend using BigDecimal
since Double
technically doesn't have any decimal values to truncate. 如果你想处理正数和负数,我建议使用
BigDecimal
因为Double
技术上没有任何十进制值来截断。 Try something like this: 尝试这样的事情:
new BigDecimal(i2[m][0]).setScale(2, BigDecimal.ROUND_FLOOR)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.