问题
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