Java有自带的 stripTrailingZeros() 方法用于去除末尾多余的0
BigDecimal num = new BigDecimal("100.1000"); BigDecimal numNoEndZero = num.stripTrailingZeros(); //numNoEndZero :1E+2 System.out.println(numNoEndZero.toString());
按上面的方法输出结果,会显示科学计数法,所以需要处理一下,解决方法:
String numNoEndZeroStr = new BigDecimal("100.1000").stripTrailingZeros().toPlainString(); //numNoEndZeroStr :100.1 System.out.println(numNoEndZeroStr);
原文地址:https://www.cnblogs.com/zjfjava/p/9931895.html
时间: 2024-10-10 07:55:32