BigDecimal with smart precision (scale) [duplicate]

假如想象 提交于 2020-07-31 04:52:03

问题


How to set generic scaling according to decimal roundness as a number

BigDecimal num = new BigDecimal(25)

BigDecimal result = num.divide(new BigDecimal(13), 7, RoundingMode.HALF_EVEN)
// 1.9230769

but

BigDecimal result = num.divide(new BigDecimal(10), 7, RoundingMode.HALF_EVEN) 
// 2.5000000 that should  be 2.5

Probably analyzing remainder?

Is there something in the API for such case?


回答1:


Look At https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#stripTrailingZeros--

BigDecimal bd = new BigDecimal("235.000");
System.out.println( bd.stripTrailingZeros() );


来源:https://stackoverflow.com/questions/46820142/bigdecimal-with-smart-precision-scale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!