BigDecimal的舍入

huangapple go评论65阅读模式
英文:

Rounding of BigDecimal

问题

我想对 Bigdecimal 进行四舍五入,例如我的 0.1,它应该四舍五入为 1。
我尝试过 Round_UP,所有的四舍五入函数。
请帮我解决。

英文:

I want to do rounding of Bigdecimal e.g my 0.1 then it should round as 1.
I tried with Round_UP, all Rounding Function
Please help me out

答案1

得分: 1

我猜你使用了 round 方法。相反地,你应该将精度改为 0

BigDecimal bd = BigDecimal.valueOf(0.1);
BigDecimal rounded = bd.setScale(0, RoundingMode.UP); // 1
英文:

I suppose you used the round method. Instead, you should change the scale to 0.

BigDecimal bd = BigDecimal.valueOf(0.1);
BigDecimal rounded = bd.setScale(0, RoundingMode.UP); // 1

huangapple
  • 本文由 发表于 2020年10月23日 17:12:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64497215.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定