java striptrailingzeros_java – 为什么不BigDecimal.stripTrailingZeros()总是删除所有尾随零?...
我做了以下事情
MathContext context = new MathContext(7, RoundingMode.HALF_UP);
BigDecimal roundedValue = new BigDecimal(value, context);
// Limit decimal places
try {
roundedValue = roundedValue.setScale(decimalPlaces, RoundingMode.HALF_UP);
} catch (NegativeArraySizeException e) {
throw new IllegalArgumentException("Invalid count of decimal places.");
}
roundedValue = roundedValue.stripTrailingZeros();
String returnValue = roundedValue.toPlainString();
如果輸入現(xiàn)在為“-0.000987654321”(=值),我返回“-0.001”(=返回值)即可.
如果輸入現(xiàn)在為“-0.0000987654321”,我會返回“-0.0001”,這也沒關(guān)系.
但是當(dāng)輸入現(xiàn)在是“-0.00000987654321”時,我得到“0.0000”而不是“0”,這是不行的.這有什么不對?為什么在這種情況下不刪除尾隨零?
總結(jié)
以上是生活随笔為你收集整理的java striptrailingzeros_java – 为什么不BigDecimal.stripTrailingZeros()总是删除所有尾随零?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MySQL中改变相邻学生座位_力扣——换
- 下一篇: MySQL宽字节注入漏洞分析_宽字节注入