当前位置:网站首页>Accuracy problem and solution of BigDecimal

Accuracy problem and solution of BigDecimal

2022-07-05 02:53:00 Bubble ^ bubble

BigDecimal take %( remainder )

package smallspring.step02;
import java.math.BigDecimal;

public class BigDecimalTest {
    
    public static void main(String[] args) {
    
        double a1 = 3.3D;
        double a2 = 0.1D;
        System.out.println("a1%a2 Residual results :"+a1 % a2);
    }
}

 Insert picture description here
terms of settlement : Use BigDecimal, Do not use new BigDecimal(), Use BigDecimal.valueof().

        System.out.println(" Use new BigDecimal:"+new BigDecimal(a1));
        System.out.println(" Use BigDecimal.valueOf:"+BigDecimal.valueOf(a1));

 Insert picture description here
divideAndRemainder: Returns the first integer in the array set , The second is the remainder , This method can be used to avoid the loss of accuracy .

BigDecimal[] values = BigDecimal.valueOf(a1).divideAndRemainder(BigDecimal.valueOf(a2));

 Insert picture description here

Tool class

package smallspring;
import java.math.BigDecimal;
/** *  because Java The simple type of can't operate on floating-point numbers accurately , This utility class provides precise floating-point operations , It includes addition, subtraction, multiplication, division and rounding . */
public class DoubleUtil {
    

    //  Default division precision 
    private static final int DEF_DIV_SCALE = 10;

    /** *  Provide precise addition operations . * @param v1  Augend  * @param v2  Addition number  * @return  The sum of two parameters  */
    public static double add(double v1, double v2) {
    
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.add(b2).doubleValue();
    }

    /** *  Provides accurate subtraction operations . * @param v1  minuend  * @param v2  Subtract  * @return  The difference between the two parameters  */
    public static double sub(double v1, double v2) {
    
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.subtract(b2).doubleValue();
    }

    /** *  Provide accurate multiplication . * @param v1  Multiplier  * @param v2  The multiplier  * @return  The product of two parameters  */
    public static double mul(double v1, double v2) {
    
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.multiply(b2).doubleValue();
    }

    /** *  Provide ( relative ) A precise division operation , When there is an inexhaustible situation , Accurate to  *  After the decimal point 10 position , The next figures are rounded off to . * @param v1  Divisor  * @param v2  Divisor  * @return  The quotient of two parameters  */
    public static double div(double v1, double v2) {
    
        return div(v1, v2, DEF_DIV_SCALE);
    }

    /** *  Provide ( relative ) A precise division operation . When there is an inexhaustible situation , from scale Parameter refers to  *  Set the accuracy , The next figures are rounded off to . * @param v1  Divisor  * @param v2  Divisor  * @param scale  The representation needs to be accurate to several decimal places . * @return  The quotient of two parameters  */
    public static double div(double v1, double v2, int scale) {
    
        if (scale < 0) {
    
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b1 = BigDecimal.valueOf(v1);
        BigDecimal b2 = BigDecimal.valueOf(v2);
        return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /** *  Provide accurate decimal rounding . * @param v  A number that needs to be rounded  * @param scale  How many decimal places to keep  * @return  The result of rounding  */
    public static double round(double v, int scale) {
    
        if (scale < 0) {
    
            throw new IllegalArgumentException("The scale must be a positive integer or zero");
        }
        BigDecimal b = BigDecimal.valueOf(v);
        BigDecimal one = new BigDecimal("1");
        return b.divide(one, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

}
原网站

版权声明
本文为[Bubble ^ bubble]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140841389694.html