java.math 包中的类:BigInteger 和 BigDecimal
- BigInteger 可以实现任意精度的整数运算。
- BigDecimal 实现任意精度的浮点数运算。
使用静态方法 valueOf 将普通的数值转换为大数:
BigInteger a = BigInteger.valueOf(100);
对于更大的数,可以使用带字符串参数的构造器:
BigInteger realliBig = new BigInteger("1323231515151323524162311");
注意:大数不能使用算数运算符,需要使用类中的 add 和 multiply 方法。
API
java.math.BigInteger
- static BigInteger valueOf(long x)
返回值等于 x 的大数
- BigInteger add(BigInteger other)
- BigInteger subtract(BigInteger other)
- BigInteger multiply(BigInteger other)
- BigInteger divide(BigInteger other)
- BigInteger sqrt()
加、减、乘、除、平方根
java.math.BigDecimal
加减乘除和 BigInteger 一样。
评论 (0)