Java学习笔记4:大数值
java.math
包含两个可以处理任意长度数字序列数值的类:BigInteger
、BigDecimal
。
BigInteger
可以实现了任意精度的整数运算,BigDecimal
实现了任意精度的浮点数运算。
注意,该类型不能使用+、-、*、/等运算符。
常用的方法如下:
类型 | 方法名称 | 作用解释 |
---|---|---|
BigInteger | add(BigInteger other) | 返回这个大整数和另一个大整数other和运算的结果 |
BigInteger | subtract(BigInteger other) | 返回这个大整数和另一个大整数other差运算的结果 |
BigInteger | multiply(BigInteger other) | 返回这个大整数和另一个大整数other积运算的结果 |
BigInteger | divide(BigInteger other) | 返回这个大整数和另一个大整数other商运算的结果 |
BigInteger | mod(BigInteger other) | 返回这个大整数和另一个大整数other取余运算的结果 |
int | compareTo(BigInteger other) | 大整数与other比较大小,大于取1,等于取0,小于取-1 |
static BigInteger | valueOf(long x) | 返回值等于x的大整数 |
类型 | 方法名称 | 作用解释 |
---|---|---|
BigDecimal | add(BigDecimal other) | 返回这个大浮点数和另一个大浮点数other和运算的结果 |
BigDecimal | subtract(BigDecimal other) | 返回这个大浮点数和另一个大浮点数other差运算的结果 |
BigDecimal | multiply(BigDecimal other) | 返回这个大浮点数和另一个大浮点数other积运算的结果 |
BigDecimal | divide(BigDecimal other) | 返回这个大浮点数和另一个大浮点数other商运算的结果 |
BigDecimal | mod(BigDecimal other) | 返回这个大浮点数和另一个大浮点数other取余运算的结果 |
int | compareTo(BigDecimal other) | 大浮点数与other比较大小,大于取1,等于取0,小于取-1 |
static BigDecimal | valueOf(long x) | 返回值等于x的大浮点数 |
static BigDecimal | valueOf(long x,int scale) | 返回值为x/(10^scale)的一个大浮点数 |