java 大数类方法整理

本文详细介绍了Java中的大数类BigInteger和BigDecimal的常用方法,包括加、减、乘、除、取余、幂运算、最大公约数、模运算以及基本操作如绝对值、取负等,并提供了示例代码.
摘要由CSDN通过智能技术生成

整形大数类:BigInteger类方法

方法名返回类型描述
add(BigInteger val)BigInteger将此BigInteger与指定的BigInteger相加
subtract(BigInteger val)BigInteger从此BigInteger中减去指定的BigInteger
multiply(BigInteger val)BigInteger将此BigInteger与指定的BigInteger相乘
divide(BigInteger val)BigInteger使用取整除法将此BigInteger除以指定的BigInteger
remainder(BigInteger val)BigInteger返回此BigInteger除以指定的BigInteger后的余数
pow(int exponent)BigInteger返回此BigInteger的exponent次幂
gcd(BigInteger val)BigInteger返回此BigInteger与指定的BigInteger的最大公约数
mod(BigInteger m)BigInteger返回此BigInteger与指定的模数m的模值
compareTo(BigInteger val)int将此BigInteger与指定的BigInteger进行比较,返回值:0相等,1大于,-1小于
equals(Object x)boolean比较此BigInteger与指定对象是否相等
abs()BigInteger返回此BigInteger的绝对值
negate()BigInteger返回此BigInteger的负值
and(BigInteger val)BigInteger对此BigInteger与指定的BigInteger进行按位与运算
or(BigInteger val)BigInteger对此BigInteger与指定的BigInteger进行按位或运算
xor(BigInteger val)BigInteger对此BigInteger与指定的BigInteger进行按位异或运算
not()BigInteger对此BigInteger进行按位非运算
shiftLeft(int n)BigInteger将此BigInteger左移n位(相当于乘以2的n次方)
shiftRight(int n)BigInteger将此BigInteger右移n位(相当于除以2的n次方)
toString()String返回此BigInteger的字符串表示形式

浮点型大数类:BigDecimal类方法

方法名返回类型描述
add(BigDecimal val)BigDecimal将此BigDecimal与指定的BigDecimal相加
subtract(BigDecimal val)BigDecimal从此BigDecimal中减去指定的BigDecimal
multiply(BigDecimal val)BigDecimal将此BigDecimal与指定的BigDecimal相乘
divide(BigDecimal divisor)BigDecimal使用指定的舍入模式对此BigDecimal进行除以指定的BigDecimal的运算,并返回结果
remainder(BigDecimal divisor)BigDecimal返回此BigDecimal除以指定的BigDecimal后的余数
pow(int n)BigDecimal返回此BigDecimal的n次幂
abs()BigDecimal返回此BigDecimal的绝对值
negate()BigDecimal返回此BigDecimal的负值
scale()int返回此BigDecimal的小数部分的位数
round(MathContext mc)BigDecimal使用指定的舍入模式对此BigDecimal进行四舍五入操作,并返回结果
compareTo(BigDecimal val)int将此BigDecimal与指定的BigDecimal进行比较,返回值:0相等,1大于,-1小于
equals(Object x)boolean比较此BigDecimal与指定对象是否相等
toString()String返回此BigDecimal的字符串表示形式

大数类常用方法示例

1. BigInteger常用方法:
add()
  • 方法名: add(BigInteger val)
  • 实现效果: 将当前BigInteger对象与指定的BigInteger对象相加。
  • 传参要求: 传入要相加的BigInteger对象。
  • 举例使用:
    BigInteger num1 = new BigInteger("12345678901234567890");
    BigInteger num2 = new BigInteger("98765432109876543210");
    BigInteger sum = num1.add(num2);
    
subtract()
  • 方法名: subtract(BigInteger val)
  • 实现效果: 从当前BigInteger对象中减去指定的BigInteger对象。
  • 传参要求: 传入要减去的BigInteger对象。
  • 举例使用:
    BigInteger num1 = new BigInteger("98765432109876543210");
    BigInteger num2 = new BigInteger("12345678901234567890");
    BigInteger diff = num1.subtract(num2);
    
multiply()
  • 方法名: multiply(BigInteger val)
  • 实现效果: 将当前BigInteger对象与指定的BigInteger对象相乘。
  • 传参要求: 传入要相乘的BigInteger对象。
  • 举例使用:
    BigInteger num1 = new BigInteger("12345678901234567890");
    BigInteger num2 = new BigInteger("98765432109876543210");
    BigInteger product = num1.multiply(num2);
    
divide()
  • 方法名: divide(BigInteger val)
  • 实现效果: 使用整数除法将当前BigInteger对象除以指定的BigInteger对象。
  • 传参要求: 传入要除以的BigInteger对象。
  • 举例使用:
    BigInteger num1 = new BigInteger("98765432109876543210");
    BigInteger num2 = new BigInteger("12345678901234567890");
    BigInteger quotient = num1.divide(num2);
    
remainder()
  • 方法名: remainder(BigInteger val)
  • 实现效果: 返回当前BigInteger对象除以指定的BigInteger对象后的余数。
  • 传参要求: 传入要求余的BigInteger对象。
  • 举例使用:
    BigInteger num1 = new BigInteger("98765432109876543210");
    BigInteger num2 = new BigInteger("12345678901234567890");
    BigInteger remainder = num1.remainder(num2);
    
2. BigDecimal常用方法:
add()
  • 方法名: add(BigDecimal val)
  • 实现效果: 将当前BigDecimal对象与指定的BigDecimal对象相加。
  • 传参要求: 传入要相加的BigDecimal对象。
  • 举例使用:
    BigDecimal num1 = new BigDecimal("123.456");
    BigDecimal num2 = new BigDecimal("987.654");
    BigDecimal sum = num1.add(num2);
    
subtract()
  • 方法名: subtract(BigDecimal val)
  • 实现效果: 从当前BigDecimal对象中减去指定的BigDecimal对象。
  • 传参要求: 传入要减去的BigDecimal对象。
  • 举例使用:
    BigDecimal num1 = new BigDecimal("987.654");
    BigDecimal num2 = new BigDecimal("123.456");
    BigDecimal diff = num1.subtract(num2);
    
multiply()
  • 方法名: multiply(BigDecimal val)
  • 实现效果: 将当前BigDecimal对象与指定的BigDecimal对象相乘。
  • 传参要求: 传入要相乘的BigDecimal对象。
  • 举例使用:
    BigDecimal num1 = new BigDecimal("123.456");
    BigDecimal num2 = new BigDecimal("987.654");
    BigDecimal product = num1.multiply(num2);
    
divide()
  • 方法名: divide(BigDecimal divisor, int scale, RoundingMode roundingMode)
  • 实现效果: 使用指定的舍入模式将当前BigDecimal对象除以指定的BigDecimal对象。
  • 传参要求: 传入要除以的BigDecimal对象、保留小数位数的scale和舍入模式roundingMode。
  • 举例使用:
    BigDecimal num1 = new BigDecimal("987.654");
    BigDecimal num2 = new BigDecimal("123.456");
    BigDecimal quotient = num1.divide(num2, 2, RoundingMode.HALF_UP);
    
remainder()
  • 方法名: remainder(BigDecimal divisor)
  • 实现效果: 返回当前BigDecimal对象除以指定的BigDecimal对象后的余数。
  • 传参要求: 传入要求余的BigDecimal对象。
  • 举例使用:
    BigDecimal num1 = new BigDecimal("987.654");
    BigDecimal num2 = new BigDecimal("123.456");
    BigDecimal remainder = num1.remainder(num2);
    

以上是Java大数类BigInteger和BigDecimal常用的方法及其使用示例。根据实际需求选择合适的方法来操作大数,以便处理超出基本数据类型范围的数值。

  • 20
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

BenChuat

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

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

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

打赏作者

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

抵扣说明:

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

余额充值