问题描述
我正在尝试删除索引9之后的所有内容(末尾的三个0)。我尝试使用 substring ,但似乎无法使其正常工作。
代码:
BigDecimal priceValue = 90.473426000;
// code to remove everything after index 9
System.out.println(priceValue);
想要的输出:
90.473426
任何帮助将不胜感激。
解决方法
我建议您不要使用String.substring
,因为它只会对输出进行外观调整。此外,BigDecimal
类中有一个方法可以为您BigDecimal#stripTrailingZeros做这件事。
BigDecimal priceValue = new BigDecimal("90.473426000");
System.out.println(priceValue);
priceValue = priceValue.stripTrailingZeros();
System.out.println(priceValue);
打印
90.473426000
90.473426