企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] # BigInteger java中long型为最大整数类型,对于超过long型的数据如何去表示呢.在Java的世界中,超过long型的整数已经不能被称为整数了,它们被封装成BigInteger对象.在BigInteger类中,实现四则运算都是方法来实现,并不是采用运算符. BigInteger类的构造方法: ![](https://box.kancloud.cn/c6c8fab33e35f9078ac51ce65ec661a0_925x328.png) 构造方法中,采用字符串的形式给出整数 **常用方法** ~~~ public BigInteger abs() public BigInteger add(BigInteger val) public BigInteger subtract(BigInteger val) public BigInteger multiply(BigInteger val) public BigInteger divide(BigInteger val) public BigInteger remainder(BigInteger val) public BigInteger pow(int exponent) public BigInteger[] divideAndRemainder(BigInteger val) ~~~ 四则运算代码: ~~~ public static void main(String[] args) { //大数据封装为BigInteger对象 BigInteger big1 = new BigInteger("12345678909876543210"); BigInteger big2 = new BigInteger("98765432101234567890"); //add实现加法运算 BigInteger bigAdd = big1.add(big2); //subtract实现减法运算 BigInteger bigSub = big1.subtract(big2); //multiply实现乘法运算 BigInteger bigMul = big1.multiply(big2); //divide实现除法运算 BigInteger bigDiv = big2.divide(big1); } ~~~ # BigDecimal double和float类型在运算中很容易丢失精度,造成数据的不准确性,Java提供我们BigDecimal类可以实现浮点数据的高精度运算 构造方法如下: ![](https://box.kancloud.cn/3e8eda4341f03f00b4b99c3fd89fbb0b_1153x803.png) 建议浮点数据以字符串形式给出,因为参数结果是可以预知的 ~~~ public static void main(String[] args) { //大数据封装为BigDecimal对象 BigDecimal big1 = new BigDecimal("0.09"); BigDecimal big2 = new BigDecimal("0.01"); //add实现加法运算 BigDecimal bigAdd = big1.add(big2); BigDecimal big3 = new BigDecimal("1.0"); BigDecimal big4 = new BigDecimal("0.32"); //subtract实现减法运算 BigDecimal bigSub = big3.subtract(big4); BigDecimal big5 = new BigDecimal("1.105"); BigDecimal big6 = new BigDecimal("100"); //multiply实现乘法运算 BigDecimal bigMul = big5.multiply(big6); } ~~~ 对于浮点数据的除法运算,和整数不同,可能出现无限不循环小数,因此需要对所需要的位数进行保留和选择舍入模式 ![](https://box.kancloud.cn/585b23a5a895ec4341b06b126e59685a_1118x95.png) ![](https://box.kancloud.cn/8a0d384bb7d1b938b39669933656a48a_1150x622.png) **常用方法** ~~~ public BigDecimal add(BigDecimal augend) public BigDecimal subtract(BigDecimal subtrahend) public BigDecimal multiply(BigDecimal multiplicand) public BigDecimal divide(BigDecimal divisor, int scale, int roundingMode) ~~~