当前位置:网站首页>Introduction to basic use and pit closure of BigDecimal

Introduction to basic use and pit closure of BigDecimal

2022-06-11 18:27:00 Rookie - work hard

Preface : We often use in the development process BigDecimal To use the amount data type , Especially keep the decimal point 2 position , Improve the accuracy of scientific calculation , however BigDecimal Improper use will also lead to loss of accuracy , And double Similar situation .

One . First, let's verify the above problem , Programmer first code

/** * @author zhaoyy * @version 1.0 * @description BigDecimal  Type used  * @date 2022/6/9 **/
public class BigDecimalTest {
    

    public static void main(String[] args) {
    
        BigDecimal bigDecimal=new BigDecimal(66);
        System.out.println(bigDecimal);
        bigDecimal=new BigDecimal("6.6");
        System.out.println(bigDecimal);
        bigDecimal=new BigDecimal(6.6);
        System.out.println(bigDecimal);

        //  Recommended 
        bigDecimal= BigDecimal.valueOf(6.6);
        System.out.println(bigDecimal);
    }
}

Output content :

66
6.6
6.5999999999999996447286321199499070644378662109375
6.6

The penultimate output above , There is a problem of precision loss , Recommended BigDecimal.valueOf() Method to complete the initialization value , Use less construction methods to BigDecimal assignment ,BigDecimal Constructors will call different constructor methods for different data types , You can click the open source code to understand the specific code . and valueOf() Method in use double Method to convert the type to toString Please see the following source code for details .

Source code display :
double Type construction method :
 Insert picture description here
 Insert picture description here
summary :
1 .BigDecimal Constructor initialization value corresponds to double,flot Type needs to consider whether it supports precision loss
2. Cause of loss of accuracy , Is that data will be converted into binary number type during data type conversion , When decimal decimal places are converted to binary, there may be an infinite loop or the length exceeding the mantissa of floating-point numbers , The problem that eventually leads to the loss of accuracy .
3. It is suggested that BigDecimal.valueOf() Try to avoid such problems

So much has been said , Let's share one BigDecimal Basic operation tool class

import java.math.BigDecimal;

/** * @author zhaoyy * @version 1.0 * @description bigDecimal  Tool class  * @date 2022/6/9 **/
public class BigDecimalUtils {
    

    public static BigDecimal doubleAdd(double v1, double v2) {
    
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.add(b2);
    }
    public static BigDecimal floatAdd(float v1, float v2) {
    
        BigDecimal b1 = new BigDecimal(Float.toString(v1));
        BigDecimal b2 = new BigDecimal(Float.toString(v2));
        return b1.add(b2);
    }
    public static BigDecimal doubleSub(double v1, double v2) {
    
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.subtract(b2);
    }
    public static BigDecimal floatSub(float v1, float v2) {
    
        BigDecimal b1 = new BigDecimal(Float.toString(v1));
        BigDecimal b2 = new BigDecimal(Float.toString(v2));
        return b1.subtract(b2);
    }

    public static BigDecimal doubleMul(double v1, double v2) {
    
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return b1.multiply(b2);
    }
    public static BigDecimal floatMul(float v1, float v2) {
    
        BigDecimal b1 = new BigDecimal(Float.toString(v1));
        BigDecimal b2 = new BigDecimal(Float.toString(v2));
        return b1.multiply(b2);
    }

    public static BigDecimal doubleDiv(double v1, double v2) {
    
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        //  Keep two decimal places  ROUND_HALF_UP =  rounding 
        return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP);
    }
    public static BigDecimal floatDiv(float v1, float v2) {
    
        BigDecimal b1 = new BigDecimal(Float.toString(v1));
        BigDecimal b2 = new BigDecimal(Float.toString(v2));
        //  Keep two decimal places  ROUND_HALF_UP =  rounding 
        return b1.divide(b2, 2, BigDecimal.ROUND_HALF_UP);
    }
    /** *  Compare v1 v2 size  * @param v1 * @param v2 * @return v1>v2 return 1 v1=v2 return 0 v1<v2 return -1 */
    public static int doubleCompareTo(double v1, double v2) {
    
        BigDecimal b1 = new BigDecimal(Double.toString(v1));
        BigDecimal b2 = new BigDecimal(Double.toString(v2));
        return  b1.compareTo(b2);
    }
    public static int floatCompareTo(float v1, float v2) {
    
        BigDecimal b1 = new BigDecimal(Float.toString(v1));
        BigDecimal b2 = new BigDecimal(Float.toString(v2));
        return  b1.compareTo(b2);
    }
}
原网站

版权声明
本文为[Rookie - work hard]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111815213645.html