Java SE-------基础知识点09(常用类(下))

大数值运算类

1. Biginter

用途:BigInteger可以表示比Integer更大的数字,是整数类型

public class Test01{
    public static void main(String[] args){
        //创建对象并赋值
        BigInteger bigInteger1=new BigInteger("12345678901234");
        BigInteger bigInteger2=new BigInteger("12345678956789");
        
        //做加法运算
        BigInteger add=bigInteger1.add(bigInteger2);
        System.out.println(add);
        
        //做减法
        BigInteger subtract=bigInteger1.subtract(bigInteger2);
        System.out.println(subtract);
        
        //做乘法
        BigInteger multiply=bigInteger1.multiply(bigInteger2);
        System.out.println(multiply);
        
        //做除法
        BigInteger divide=bigInteger1.divide(bigInteger2);
        System.out.println(divide);
    }
}

2. BigDecemal

用途:作用于浮点数的计算,小数计算做除法时,一定要设置保留几位小数以及取舍方法,是向上进去还是向下又或者四舍五入,一定要写上。

public class Test01{
    public static void main(String[] args){
        /**
        浮点型大值运算
        */
        BigDecimal bigDecimal1=new BigDecimal("0.5");
        BigDecimal bigDecimal2=new BigDecimal("0.4");

        BigDecimal add = bigDecimal1.add(bigDecimal2);//加法
        System.out.println(add);

        BigDecimal subtract = bigDecimal1.subtract(bigDecimal2);//减法
        System.out.println(subtract);

        BigDecimal multiply = bigDecimal1.multiply(bigDecimal2);//乘法
        System.out.println(multiply);

        BigDecimal divide = bigDecimal1.divide(bigDecimal2);//除法
        System.out.println(divide);
    }
}

注意:

public class Test01{
    public static void main(String[] args){
        /**
         * 注意:小数除法一定要设置保留几位小数 和 进制模式
         */

        BigDecimal big1 = new BigDecimal("10");
        BigDecimal big2 = new BigDecimal("3");

        //除法
        BigDecimal divide = big1.divide(big2, 2, BigDecimal.ROUND_HALF_UP);
        System.out.println(divide);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

榮十一

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值