java大数(BigInteger 和 BigDecimal):

大数:
如果基本的整数和浮点数精度不能够满足需求,那么可以使用java.math包中两个很有用的类:
BigInteger 和 BigDecimal。这两个类可以处理包含任意长度数字序列的数值。
BigInteger类实现任意精度的 整数运算
BigDecimal类实现任意精度的 浮点数运算
BigInteger bigInteger = new BigInteger("412322312312121212121");
//1.在对 BigInteger进行加减乘除的时候,要使用对应的方法,不能直接+ - * /。
//2.  add       加
      substract 减
      multiply  乘
      divide    除
BigDecimal bigDecimal = new BigDecimal("45654545646546.1212121212121");
BigDecimal bigDecimal1 = new BigDecimal("1.1");
//这里会抛出ArithmeticException
//原因是 运算的结果是一个无限循环小数,无法被除尽
//System.out.println(bigDecimal.divide(bigDecimal1));
//解决方法:
//在调用divide 方法时,使用 RoundingMode.CEILING 指定精度即可
//如果有无限循环小数时,就会保留 分子 的精度
System.out.println(bigDecimal.divide(bigDecimal1 , RoundingMode.CEILING));

代码示例: 


	 import java.math.*;

public class BigNumber{
	
    public static void main(String[] args) {
	
        BigInteger bigInteger = new BigInteger("412322312312121212121");
        BigInteger bigInteger2 = new BigInteger("99");
	
        BigInteger add = bigInteger.add(bigInteger2);              //加
        BigInteger subtract = bigInteger.subtract(bigInteger2);    //减
        BigInteger multiply = bigInteger.multiply(bigInteger2);    //乘
        BigInteger divide = bigInteger.divide(bigInteger2);        //除
	
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值