在ACM中Java关于大数的一些相关操作

BigInteger和BigDecimal可以说是acmer选择java的首要原因。
函数:add, subtract, divide, mod, compareTo等,其中加减乘除模都要求是BigInteger(BigDecimal)和BigInteger(BigDecimal)之间的运算,所以需要把int(double)类型转换为BigInteger(BigDecimal),用函数BigInteger.valueOf().

BigInteger

主要API

将字符串转换成BigInteger

BigInteger(Stringval)
将 BigInteger 的十进制字符串表示形式转换为 BigInteger。

BigInteger(Stringval, intradix)
将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger。

BigInteger的加法

BigInteger

add(BigIntegerval)
返回其值为 (this + val) 的 BigInteger。

BigInteger

and(BigIntegerval)
返回其值为 (this & val) 的 BigInteger。

BigInteger的减法

BigInteger

subtract(BigIntegerval)
返回其值为 (this - val) 的 BigInteger。

BigInteger的乘法

BigInteger

multiply(BigIntegerval)
返回其值为 (this * val) 的 BigInteger。

大数求余:

BigInteger

mod(BigIntegerm)
返回其值为 (this mod m) 的 BigInteger。

大数除法

BigInteger

divide(BigIntegerval)
返回其值为 (this / val) 的 BigInteger。

其他一些

BigInteger

gcd(BigIntegerval)
返回一个 BigInteger,其值是 abs(this) 和 abs(val) 的最大公约数。

BigInteger

max(BigIntegerval)
返回此 BigInteger 和 val 的最大值。

BigInteger

min(BigIntegerval)
返回此 BigInteger 和 val 的最小值。

BigDecimal类

主要API:

将字符串转换成BigDecimal

BigDecimal(Stringval)
将 BigDecimal 的字符串表示形式转换为 BigDecimal。

BigDecimal(Stringval, MathContextmc)
将 BigDecimal 的字符串表示形式转换为 BigDecimal,接受与 BigDecimal(String) 构造方法相同的字符串(按照上下文设置进行舍入)。

两个BigDecimal的相加

BigDecimal

add(BigDecimalaugend)
返回一个 BigDecimal,其值为 (this + augend),其标度为 max(this.scale(), augend.scale())。

BigDecimal

add(BigDecimalaugend, MathContextmc)
返回其值为 (this + augend) 的 BigDecimal(根据上下文设置进行舍入)。

两个BigDecimal的相减

BigDecimal

subtract(BigDecimalsubtrahend)
返回一个 BigDecimal,其值为 (this - subtrahend),其标度为 max(this.scale(), subtrahend.scale())。

BigDecimal

subtract(BigDecimalsubtrahend, MathContextmc)
返回其值为 (this - subtrahend) 的 BigDecimal(根据上下文设置进行舍入)。

两个BigDecimal的相除:

BigDecimal

divide(BigDecimaldivisor)
返回一个 BigDecimal,其值为 (this / divisor),其首选标度为 (this.scale() - divisor.scale());如果无法表示准确的商值(因为它有无穷的十进制扩展),则抛出 ArithmeticException。

BigDecimal

divide(BigDecimaldivisor, introundingMode)
返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。

BigDecimal

divide(BigDecimaldivisor, intscale, introundingMode)
返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。

BigDecimal

divide(BigDecimaldivisor, intscale, RoundingModeroundingMode)
返回一个 BigDecimal,其值为 (this / divisor),其标度为指定标度。

BigDecimal

divide(BigDecimaldivisor, MathContextmc)
返回其值为 (this / divisor) 的 BigDecimal(根据上下文设置进行舍入)。

BigDecimal

divide(BigDecimaldivisor, RoundingModeroundingMode)
返回一个 BigDecimal,其值为 (this / divisor),其标度为 this.scale()。

计算BigDecimal的N次幂

BigDecimal

pow(intn)
返回其值为 (thisn) 的 BigDecimal,准确计算该幂,使其具有无限精度。

BigDecimal

pow(intn, MathContextmc)
返回其值为 (thisn) 的 BigDecimal。

有关转换成字符串的方法

String

toEngineeringString()
返回此 BigDecimal 的字符串表示形式,需要指数时,则使用工程计数法。

String

toPlainString()
返回不带指数字段的此 BigDecimal 的字符串表示形式。

String

toString()
返回此 BigDecimal 的字符串表示形式,如果需要指数,则使用科学记数法。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值