【问题标题】:Deleting characters after based on index on a BigDecimal根据 BigDecimal 上的索引删除字符
【发布时间】:2020-09-10 23:21:18
【问题描述】:

我正在尝试删除索引 9 之后的所有内容(末尾的三个 0)。我曾尝试使用 substring 但似乎无法正常工作。

代码:

BigDecimal priceValue = 90.473426000;

// code to remove everything after index 9

System.out.println(priceValue);

想要的输出:

90.473426

任何帮助将不胜感激。

【问题讨论】:

标签: java indexing substring bigdecimal


【解决方案1】:

我建议不要为此使用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

【讨论】:

    猜你喜欢
    • 2019-12-22
    • 1970-01-01
    • 2012-11-16
    • 2019-12-02
    • 2018-05-06
    • 2020-01-15
    • 2015-07-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多