BigInteger和BigDecimal类

一、应用场景

1. BigInteger 类

目前,我们学过最大的是long类型,但是,在实际开发时候,很有可能遇见超出long类型范围的数,我们就需要用BigInteger类;

① add    加

② subtract    减

③ multiply   乘

④ divide   除

public class BigInteger_ {
    public static void main(String[] args) {
        BigInteger bigInteger = new BigInteger("299999999999999999999999999999999");
        System.out.println(bigInteger);
//        在对BigInteger类型的数据进行 加减乘除 时,不能直接+ - * /
//        要使用相应的方法
        BigInteger bigInteger1 = new BigInteger("100");
//        1.加
        BigInteger add = bigInteger.add(bigInteger1);
        System.out.println(add);
//        2.减
        BigInteger subtract = bigInteger.subtract(bigInteger1);
        System.out.println(subtract);
//        3.乘
        BigInteger multiply = bigInteger.multiply(bigInteger1);
        System.out.println(multiply);
//        4.除
        BigInteger divide = bigInteger.divide(bigInteger1);
        System.out.println(divide);
    }
}

 

 2. BigDecimal 类

目前,我们学过精度最高的是double类型,但是,在实际开发时候,很有可能需要精度更高的类型,我们就需要用BigDecimal类; 

public class BigDecimal_ {
    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("2.99999999999999999999999999999999");
        System.out.println(bigDecimal);
//        在对BigDecimal类型的数据进行 加减乘除 时,不能直接+ - * /
//        要使用相应的方法
        BigDecimal bigDecimal1 = new BigDecimal("1.63");
//        1.加
        BigDecimal add = bigDecimal.add(bigDecimal1);
        System.out.println(add);
//        2.减
        BigDecimal subtract = bigDecimal.subtract(bigDecimal1);
        System.out.println(subtract);
//        3.乘
        BigDecimal multiply = bigDecimal.multiply(bigDecimal1);
        System.out.println(multiply);
//        4.除
        BigDecimal divide = bigDecimal.divide(bigDecimal1);//可能会抛出异常
//        在调用divide 方法时,指定精度即可
//        如果有无限循环小数,就会保留  分子  的精度
        BigDecimal divide1 = bigDecimal.divide(bigDecimal1,BigDecimal.ROUND_CEILING);
        System.out.println(divide1);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

徐晓率

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

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

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

打赏作者

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

抵扣说明:

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

余额充值