简体   繁体   English

如何使用BigDecimal查找小数点后的位数?

[英]How do You Find the Number of Places After the Decimal Point with BigDecimal?

I'm trying to use BigDecimals to calculate something and then round to 5 decimal places if it has more than that. 我正在尝试使用BigDecimals来计算某些内容,然后将其四舍五入到小数点后5位。 How can I do this? 我怎样才能做到这一点?

Would scale() work? scale()会起作用吗?

Did you already crawl through JavaDoc, especially the function precision() ? 您是否已经通过JavaDoc进行了爬网,特别是函数precision()

And this here might be a direct solution which makes it needless to check the precision first: 这可能是直接解决方案,无需首先检查精度:

yournumber.round(new MathContext(5, HALF_UP));

And after that use stripTrailingZeros() (thx to @GregKopff) 然后使用stripTrailingZeros()(@GregKopff)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM