[英]How to truncate number before decimal?
Below code truncate number after decimal 下面的代码截断小数后的数字
NumberFormat blah= new DecimalFormat("#.##");
But I want to truncate extra zeros from the number which are present before number 但我想从数字之前的数字中截断额外的零
eg 例如
0000000000000.00 -> 0.00
00000100000.00 -> 100000.00
000000175000.00 -> 175000.00
BigDecimal myNumber = new BigDecimal("00000000175000.000000");
BigDecimal truncatedNumber = myNumber.setScale(3, BigDecimal.ROUND_HALF_UP);
You don't need the number format unless you want to print it out that way. 除非您想以这种方式打印,否则您不需要数字格式。 Just take the string and use Double.parseDouble()
, then print the format: 只需取字符串并使用Double.parseDouble()
,然后打印格式:
// "#.00" always shows 2 digits after decimal, "#.##" is optional if zeros are after decimal
NumberFormat blah = new DecimalFormat("#.00");
String s = "00000100000.00";
System.out.println(blah.format(Double.parseDouble(s)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.