欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

BigDecimal运算(加、减、乘、除)

程序员文章站 2022-06-20 09:42:36
public class BigDecimalOperation { private BigDecimalOperation(){ } public static BigDecimal add(double v1,double v2){ BigDecimal b1 = new BigDecimal(... ......
public class bigdecimaloperation {

    private bigdecimaloperation(){

    }


    public static bigdecimal add(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 sub(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 mul(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 div(double v1,double v2){
        bigdecimal b1 = new bigdecimal(double.tostring(v1));
        bigdecimal b2 = new bigdecimal(double.tostring(v2));
        return b1.divide(b2,2,bigdecimal.round_half_up);//四舍五入,保留2位小数
    }

}