如果基本的整数和浮点数精度不能满足需求,可以使用java.math包中BigInteger和BigDecimal。
BigInterger实现了任意精度的整数运算,BigDecimal实现了任意精度的浮点是运算
BIgInterger 不能使用运算符(+和*)处理大数值,常用方法:
BIgInterger valueOf(Long val) :将普通的数值转换成大数值
BIgInterger add(BIgInterger other) :返回这个大整数和大整数other的和
BigInterger subtract(BigInterger other):返回这个大整数和大整数other的差
BigInterger multiply(BigInterger other):返回这个大整数和大整数other的积
BigInterger divide(BigInterger other):返回这个大整数和大整数other的商
BigInterger mod(BigInterger other):返回这个大整数和大整数other的余数
int compareTo(BigInterger other):如果这个大整数和另外一个大整数other相等,返回0;如果这个大整数鱼鱼另外一个大整数other返回负数,否则返回正数。
BigDecimal常用方法:
BigDecimal add(BigDecimal other) :返回这个大实数和大实数other的和
BigDecimal subtract(BigDecimal other):返回这个大实数和大实数other的差
BigDecimal multiply(BigDecimal other):返回这个大实数和大实数other的积
BigDecimal divide(BigDecimal other, RoundingMode mode):返回这个大实数和大实数other的商,必须给出四舍五入的方式(RoundingMode)
static BigDecimal valueOf(long x):返回值为X的大实数
static BigDecimal valueOf(long x,int 2):返回x/10² (2是变量)