BigDecimal precision problem and remove the "0" after it

Prerequisite: Use the hutool tool

		<!-- hutool工具类 -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.5.1</version>
        </dependency>

1. Remove the 0 behind, and get BigDecimal type data

BigDecimal num = NumberUtil.toBigDecimal(10.0000);
System.out.println(NumberUtil.toBigDecimal(NumberUtil.toStr(num)));

Display: 10
If the last thing you want is other types of data, you can change to NumberUtil.to other types

2. You can use the above method to obtain the modified BigDecimal data and then perform calculations

// 除数可能存在除不尽的情况,设定获取多少位数,并且去掉后面无用的0
NumberUtil.toBigDecimal(NumberUtil.toStr(num.divide(NumberUtil.toBigDecimal(10), 8, RoundingMode.HALF_UP)));

Guess you like

Origin blog.csdn.net/qq_45850872/article/details/111379642