BigDecimal 比较大小

        BigDecimal aaa = new BigDecimal (12.55555).setScale(3, BigDecimal.ROUND_HALF_UP);
        BigDecimal bbb = new BigDecimal (11.44444).setScale(3, BigDecimal.ROUND_HALF_UP);

        if(aaa.compareTo(bbb) == -1){//aaa<bbb
            System.out.println("a小于b");
        }
        if(aaa.compareTo(bbb) < 1){//aaa<=bbb
            System.out.println("a小于等于b");
        }

        if(aaa.compareTo(bbb) == 0){//aaa==bbb
            System.out.println("a等于b");
        }

        if(aaa.compareTo(bbb) == 1){//aaa>bbb
            System.out.println("a大于b");
        }

        if(aaa.compareTo(bbb) > -1){//aaa>=bbb
            System.out.println("a大于等于b");
        }

 

加减乘除
        BigDecimal bignum1 = new BigDecimal("10");
        BigDecimal bignum2 = new BigDecimal("5");
        BigDecimal bignum3 = null;
        
        //加法
        bignum3 =  bignum1.add(bignum2);//bignum1+bignum2
        System.out.println("求和:" + bignum3);
        
        //减法
        bignum3 = bignum1.subtract(bignum2);//bignum1-bignum2
        System.out.println("求差:" + bignum3);

        //乘法
        bignum3 = bignum1.multiply(bignum2);//bignum1*bignum2
        System.out.println("乘法积:" + bignum3);
        //除法
        bignum3 = bignum1.divide(bignum2);//bignum1/bignum2
        System.out.println("除法结果:" + bignum3);

 

posted on 2020-09-11 10:07  秦岭过客  阅读(272)  评论(0编辑  收藏  举报