BigInteger类
不可以改变的任意精度的整数,底层使用的字符串
进行实现的,int类型的“+、-、*、/
”等在这里都不能使用
,但是里面包装了相关的运算的方法。
- BigInteger(String val):通过字符串生成一个BigInteger对象
- BigInteger add(BigInteger val):将两个BigInteger对象进行相加运算,返回值也是一个BigInteger对象
- BigInteger subtract(BigInteger val):将两个BigInteger对象进行相减运算,返回值也是一个BigInteger对象
- BigInteger multiply(BigInteger val):将两个BigInteger对象进行乘法运算,返回值也是一个BigInteger对象
- BigInteger divide(BigInteger val):将两个BigInteger对象进行除法运算,返回值也是一个BigInteger对象
- BigInteger remainder(BigInteger val):取余,将两个BIgInteger对象进行取余运算,返回值也是一个BigInteger对象
- int intValue():将BigInteger对象转换为一个int类型的数
- long longValue():将BigInteger对象转换为一个long类型的数
- float floatValue():将BigInteger对象转换为一个float类型的数
- …
代码实例:
public static void main(String[] args) {
long longNum = 457654778540878935L;
BigInteger bigInteger1 = new BigInteger(String.valueOf(longNum));
System.out.println("bigInteger1 = " + bigInteger1);
BigInteger bigInteger2 = new BigInteger(String.valueOf(longNum));
System.out.println("bigInteger2 = " + bigInteger2);
BigInteger add = bigInteger1.add(bigInteger2);
System.out.println("add = " + add);
BigInteger subtract = bigInteger1.subtract(bigInteger2);
System.out.println("subtract = " + subtract);
BigInteger multiply = bigInteger1.multiply(bigInteger2);
System.out.println("multiply = " + multiply);
BigInteger divide = bigInteger1.divide(bigInteger2);
System.out.println("divide = " + divide);
BigInteger remainder = bigInteger1.remainder(bigInteger2);
System.out.println("remainder = " + remainder);
int intValue = bigInteger1.intValue();
System.out.println("intValue = " + intValue);
double doubleValue = bigInteger1.doubleValue();
System.out.println("doubleValue = " + doubleValue);
float floatValue = bigInteger1.floatValue();
System.out.println("floatValue = " + floatValue);
long longValue = bigInteger1.longValue();
System.out.println("longValue = " + longValue);
}
结果:
BigDecimal类
不可变的任意精度的有符号十进制数。底层也是使用的字符串
进行实现的。
在我们平常使用double的时候经常会碰见精度问题,比如我们传入的12323.20 或许会变成12323.199999999,这些是double类型的精度问题,我们不可避免,但是BigDecimal
就能够很好的避免这样的问题。和BigInteger
代表任意大小的整型一样,BigDecimal
代表了任意精度、任意大小的十进制数,只要你的内存够大,它就能足够大。
常用API:
- BigDecimal(String val):将BigDecimal的字符串表示 BigDecimal转换为 BigDecimal 。
- BigDecimal(BigInteger val) :将 BigInteger转换成 BigDecimal 。
- BigDecimal add(BigDecimal val) :传入一个BigDecimal对象,将两者进行相加,返回给BigDecimal类型引用
- BigDecimal subtract(BigDecimal val):传入一个BigDecimal类型,两者进行相减,返回一个BigDecimal类型对象。
- BigDecimal multiply(BigDecimal val) :将两个BigDecimal对象进行相乘,返回一个BigDecimal对象
- BigDecimal divide(BigDecimal val) :将两个BigDecimal对象进行相除,返回一个BigDecimal对象
- BigDecimal divide(BigDecimal divisor, int roundingMode) :将两个BigDecimal对象进行相除,返回一个BigDecimal对象,roundingMode表示指定的舍入模式
- BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) :将两个BigDecimal对象进行相除,返回一个BigDecimal对象,scale表示取几位小数,roundingMode表示指定的舍入模式
- BigDecimal remainder(BigDecimal val): 将两个BigDecimal对象进行取余,返回一个BigDecimal对象
- double doubleValue():将此 BigDecimal 转换为 double。
- …
代码实例:
public static void main(String[] args) {
long longNum = 457654778540878935L;
BigDecimal bigDecimal = new BigDecimal(longNum);
BigDecimal bigDecimal2 = new BigDecimal(String.valueOf(3));
System.out.println("bigDecimal = " + bigDecimal);
System.out.println("bigDecimal = " + bigDecimal);
BigDecimal add = bigDecimal.add(bigDecimal);
System.out.println("add = " + add);
BigDecimal subtract = bigDecimal.subtract(bigDecimal);
System.out.println("subtract = " + subtract);
BigDecimal multiply = bigDecimal.multiply(bigDecimal);
System.out.println("multiply = " + multiply);
BigDecimal divide = bigDecimal.divide(bigDecimal2);
System.out.println("divide = " + divide);
BigDecimal divideRoundMode = bigDecimal.divide(bigDecimal2, BigDecimal.ROUND_UP); //进位
System.out.println("divideRoundMode = " + divideRoundMode);
BigDecimal divideScale = bigDecimal.divide(bigDecimal2, 3, BigDecimal.ROUND_DOWN); //去除多位
System.out.println("divideRoundScale = " + divideScale);
BigDecimal remainder = bigDecimal.remainder(bigDecimal);
System.out.println("remainder = " + remainder);
int intValue = bigDecimal.intValue();
System.out.println("intValue = " + intValue);
double doubleValue = bigDecimal.doubleValue();
System.out.println("doubleValue = " + doubleValue);
float floatValue = bigDecimal.floatValue();
System.out.println("floatValue = " + floatValue);
long longValue = bigDecimal.longValue();
System.out.println("longValue = " + longValue);
}
结果:
除此之外: BigInteger和BigDecimal类中还包括了
- abs()
- compareTo(BigDecimal val)
- pow(int n)
- max(BigDecimal val)
- min(BigDecimal val)
- pow(int n)
- …
- 由于方法过多,这里就不列举了。
注意:
在包装类中除了Float类和Double类没有缓冲区,在这里BigInteger
也有一个类似的缓冲区,Integer
的缓冲区大小是 -128到127,而BigInteger
的缓冲区大小为 -16到16。
缓冲区:
在缓冲区数值范围内的会直接在内存中取出对象,超过范围的会创建新的对象。