Java中的高精度数值

Java中的大数值

在Java中,如果基本的整数Integer和浮点数Double的精度不能满足需求,那么可以使用java.math包中的两个类:BigIntegerBigDecimal。这两个类可以处理包含任意长度数字序列的数值。BigInteger类实现了任意精度的整数运算,BigDecimal实现了任意精度的浮点数运算。

BigInteger

// 对于BigInteger不能使用算术运算符直接处理,而需要使用以下方法
BigInteger add(BigInteger other)// 和
BigInteger subtract(BigInteger other)// 差
BigInteger multiply(BigInteger other)// 积
BigInteger divide(BigInteger other)// 商
BigInteger mod(BigInteger other)// 余数
// 如果这个大整数与另一个大整数other相等,返回0;如果小于,返回负数;否则返回正数
int compareTo(BigInteger other)
// 返回值等于x的大整数
static BigInteger valueOf(long x)

BigDecimal

// 同样,BigDecimal也不能直接使用运算符运算
BigDecimal add(BigDecimal other)// 和
BigDecimal subtract(BigDecimal other)// 差
BigDecimal multiply(BigDecimal other)// 积
// 商,在计算商时,必须给出舍入方式(rounding mode),RoundingMode是枚举类型,常用的是RoundingMode.HALF_UP(四舍五入)
BigDecimal divide(BigDecimal other, RoundingMode mode)
// 如果这个大实数与另一个大实数other相等,返回0;如果小于,返回负数;否则返回正数
int compareTo(BigDecimal other)
// 返回值为x的大实数
static BigDecimal valueOf(long x)
static BigDecimal valueOf(double x)
// 返回值为x/(10^scale)的大实数
static BigDecimal valueOf(long x, int scale)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值