Java学习笔记 --- BigInteger和BigDecimal类

一、基本介绍

1、BigInteger 适合保存比较大的整型

2、BigDecimal 适合保存精度更高的浮点型(小数)

二、BigInteger常见方法

1、add:加

2、subtract:减

3、multiply:乘

4、divide:除

public class BigInteger_ {
    public static void main(String[] args) {
        //当我们编程中,需要处理很大的整数
        //可以使用BigInteger
        BigInteger bigInteger = new BigInteger("99999999999999999999");
        BigInteger bigInteger1 = new BigInteger("1000");
        System.out.println(bigInteger);
        //在对 BigInteger 加减乘除的时候,需要对应的方法 不能直接 + - * /
        //创建一个需要操作的 BigInteger 进行加减乘除
        BigInteger n1 = bigInteger.add(bigInteger1);//加
        System.out.println(n1);

        BigInteger n2 = bigInteger.subtract(bigInteger1);//减
        System.out.println(n2);

        BigInteger n3 = bigInteger.multiply(bigInteger1);//乘
        System.out.println(n3);

        BigInteger n4 = bigInteger.divide(bigInteger1);//除
        System.out.println(n4);
    }
}

三、BigDecimal常见方法

1、add:加

2、subtract:减

3、multiply:乘

4、divide:除

public class BigDecimal_ {
    public static void main(String[] args) {
        //当我们编程中,需要处理很大的整数
        //可以使用BigInteger
        BigDecimal bigDecimal = new BigDecimal("9.9999999999999999999999");
        BigDecimal bigDecimal1 = new BigDecimal("1.12");
        System.out.println(bigDecimal);
        //在对 BigDecimal 加减乘除的时候,需要对应的方法 不能直接 + - * /
        //创建一个需要操作的 BigInteger 进行加减乘除
        BigDecimal n1 = bigDecimal.add(bigDecimal1);//加
        System.out.println(n1);

        BigDecimal n2 = bigDecimal.subtract(bigDecimal1);//减
        System.out.println(n2);

        BigDecimal n3 = bigDecimal.multiply(bigDecimal1);//乘
        System.out.println(n3);

        //有可能抛出异常
        //在调用divide方法时,指定精度即可 BigDecimal.ROUND_CEILING
        //如果无限循环 就会保留分子的精度
        BigDecimal n4 = bigDecimal.divide(bigDecimal1, BigDecimal.ROUND_CEILING);//除
        System.out.println(n4);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值