Precautions for using Bigdecimal

    /**
     * Note on the problem:
     * 0. There are several ways to construct
     * 1. Addition, subtraction, multiplication and division
     * 2. Accuracy issues
     * 3. Compare the size
     */


    /**
     * Set precision:
     * The BigDecimal.setScale() method is used to format the decimal point
     * setScale(1) means to keep one decimal place, the default is rounding
     * setScale(1, BigDecimal.ROUND_DOWN) deletes the extra decimal places directly, such as 2.35 will become 2.3
     * setScale(1, BigDecimal.ROUND_UP) carry processing, 2.35 becomes 2.4
     * setScale(1,BigDecimal.ROUND_HALF_UP) rounds up, 2.35 becomes 2.4
     * setScaler(1,BigDecimal.ROUND_HALF_DOWN) rounds up, 2.35 becomes 2.3, if it is 5, it rounds down
     */


    /**
     * Comparison of size:
     * 1.compareTo()
     * 2.equels(): Not only the values ​​are equal, but also the precision range is the same, for example: 2.0 and 2.00 are not equal
     * */

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326404399&siteId=291194637