在java.math包中有两个常用的类:
BigInteger:可以存储任意精度的整数数值
BigDecimal:可以存储任意精度的浮点数值
不过,大数运算不能使用“+、-、*、/”等常用的运算符,而是使用“方法”(Java没有提供运算符重载)。
普通数值参加运算,需要进行转换。例如:
BigInteger a=BigInteger.valueOf(100);
下面用一个复杂点的例子解释一下:
用大数计算:lotteryOdds=lotteryOdds*(n-1+1)/i;
语句为:lotteryOdds=lotteryOdds.multiply(BigInteger.valueOf(n-i+1)).divide(BigInteger.valueOf(i));