大数
一:整形大数二:浮点型大数
若基本的整形和浮点型精度不够满足需求,那么可以使用java.math包中两个很有用的类:BigInteger和BigDecimal。这两个类可以处理包含任意长度数字序列的数值。BigInteger类实现任意精度的整数运算,BigDecimal实现任意精度的浮点数运算。
构造方法:
//构造方法一 BigInteger bigInteger = new BigInteger(String str); //构造方法二 BigInteger bigInteger = BigInteger.valueOf(int x);
常用API:
//实现加法运算 BigInteger add(BigInteger other) //减法运算 BigInteger subtract(BigInteger other) //乘法运算 BigInteger multiply(BigInteger other) //除法运算 BigInteger divide(BigInteger other) //取模运算 BigInteger mod(BigInteger other) //与另一个大数相比较,若相等返回0,小返回-1,大返回1 int compareTo(BigInteger other)' //将基本的整形变成整形大数,返回值等于x的大数 static BigInteger valueOf(long x)
代码示例:
public static void main(String[] args) {
//整形大数
//构造方法一
BigInteger bigInteger = new BigInteger("1234567890234321");
System.out.println(bigInteger);
//构造方法二
BigInteger bigInteger1 = BigInteger.valueOf(100);
System.out.println(bigInteger1);
//加:1234567890234321+100+100
BigInteger sum = bigInteger.add(bigInteger1).add(BigInteger.valueOf(100));
System.out.println(sum);
//减1234567890234321-1234567890234321-100 = -100
BigInteger sub = bigInteger.subtract(bigInteger).subtract(BigInteger.valueOf(100));
System.out.println(sub);
//乘
BigInteger mut = bigInteger.multiply(BigInteger.valueOf(0)).add(bigInteger1);
System.out.println(mut);
//除
BigInteger divide = bigInteger.divide(bigInteger);
System.out.println(divide);
//取模
BigInteger mod = bigInteger.mod(BigInteger.valueOf(2));
System.out.println(mod);
//比较
int i = bigInteger1.compareTo(BigInteger.valueOf(100));
System.out.println(i);
int i1 = bigInteger1.compareTo(BigInteger.valueOf(90));
System.out.println(i1);
int i2 = bigInteger1.compareTo(BigInteger.valueOf(110));
System.out.println(i2);
}
结果:
构造方法:
BigDecimal bigDecimal = new BigDecimal(int x);
BigDecimal bigDecimal1 = new BigDecimal(double x);
BigDecimal bigDecimal2 = new BigDecimal(String str);
BigDecimal bigDecimal3 = new BigDecimal(BigInteger integer);
常用API:
BigDecimal add(BigDecimal other) //减法运算 BigDecimal subtract(BigDecimal other) //乘法运算 BigDecimal multiply(BigDecimal other) //除法运算 //如果商是个寻仙循环的小数时,程序会抛出异常 BigDecimal divide(BigDecimal other) //得到一个舍入的结果,如果是四舍五入,那么第二个参数是RoundingMode.HALF_UP BigDecimal divide(BigDecimal other,RoundingMode mode) //与另一个大数相比较,若相等返回0,小返回-1,大返回1 int compareTo(BigDecimal other)' //将基本的整形变成整形大数,返回值等于x的大数 static BigDecimal valueOf(long x)
代码示例:
//构造方式
BigDecimal bigDecimal = new BigDecimal(1000);
BigDecimal bigDecimal1 = new BigDecimal(100.1);
BigDecimal bigDecimal2 = new BigDecimal("10002");
BigDecimal bigDecimal3 = new BigDecimal(bigInteger1);
System.out.println(bigDecimal);
System.out.println(bigDecimal1);
System.out.println(bigDecimal2);
System.out.println(bigDecimal3);
//加
BigDecimal add = bigDecimal.add(bigDecimal1);
System.out.println(add);
//减
BigDecimal subtract = bigDecimal.subtract(bigDecimal1);
System.out.println(subtract);
//乘
BigDecimal multiply = bigDecimal.multiply(BigDecimal.valueOf(2));
System.out.println(multiply);
//除
// BigDecimal divide1 = bigDecimal.divide(BigDecimal.valueOf(3));//结果是一个无限循环的小数,这种方法会报错
BigDecimal divide2 = bigDecimal.divide(BigDecimal.valueOf(3), RoundingMode.UP);//对于无限循环的小数进行四舍五入
// System.out.println(divide1);
System.out.println(divide2);
//比较
int i3 = bigDecimal.compareTo(BigDecimal.valueOf(1000));
int i4 = bigDecimal.compareTo(BigDecimal.valueOf(1));
int i5 = bigDecimal.compareTo(BigDecimal.valueOf(1000000));
System.out.println(i3);
System.out.println(i4);
System.out.println(i5);
结果:



