开发过程中肯定会经常遇到对于大数据的计算,Java本身的数据类型,最大的long和double很可能满足不了需求,这个时候,就需要使用BigInteger或BigDecimal类。
BigInteger类的主要构造器和方法
构造器描述
BigInteger(String) 创建一个具有参数所指定以字符串表示的数值的对象。
方法描述
BigInteger add(BigInteger other) BigInteger对象中的值相加,然后返回这个对象。
BigInteger subtract(BigInteger other) BigInteger对象中的值相减,然后返回这个对象。
BigInteger multiply(BigInteger other) BigInteger对象中的值相乘,然后返回这个对象。
BigInteger divide(BigInteger other) BigInteger对象中的值相除,然后返回这个对象。
BigInteger mod(BigInteger other) BigInteger对象中的值求余,然后返回这个对象。
int compareTo(BigInteger other) 如果这个大整数与另一个大整数other相等,返回0;这个大整数<other,返回负 数;否则返回正数
static BigInteger valueOf(long x) 返回值等于x的大整数
BigDecimal类的主要构造器和方法
构造器描述
BigDecimal(int) 创建一个具有参数所指定整数值的对象。
BigDecimal(double) 创建一个具有参数所指定双精度值的对象。
BigDecimal(long) 创建一个具有参数所指定长整数值的对象。
BigDecimal(String) 创建一个具有参数所指定以字符串表示的数值的对象。
方法描述
BigDecimal add(BigDecimal other) BigDecimal对象中的值相加,然后返回这个对象。
BigDecimal subtract(BigDecimal other) BigDecimal对象中的值相减,然后返回这个对象。
BigDecimal multiply(BigDecimal other) BigDecimal对象中的值相乘,然后返回这个对象。
BigDecimal divide(BigDecimal other) BigDecimal对象中的值相除,然后返回这个对象。
BigDecimal divide(BigDecimal other,int roundingMode) BigDecimal对象中的值相除,然后返回这个对象。 要想计算商,必需给出舍入方式(rounding mode)。RoundingMode.HALF_UP是四舍五入的方式。适用于常规的计算。其他舍入方式请参看RoundingMode的API文档。
int compareTo(BigInteger other) 如果这个大实数与另一个大实数other相等,返回0;这个大实数<other,返回负数;否则返回正数
double doublueValue() 将BigDecimal对象中的值以双精度数返回。
float floatValue() 将BigDecimal对象中的值以单精度数返回。
long longValue() 将BigDecimal对象中的值以长整数返回。
int intValue() 将BigDecimal对象中的值以整数返回。
static BigDecimal valueOf(long x) 返回值为x的一个大实数
static BigDecimal valueOf(long x,int sccale) 返回值为x/10^scale的一个大实数
参考博文:http://blog.csdn.net/w627782664/article/details/7064243