一、大数字运算
在 Java 中提供了大数字的操作类,即 java.math.BigInteger 类与 java.math.BigDecimal 类。这两个类用于高精度计算,体重 BigInteger 类是针对大整数的处理类,而 BigDecimal 类则是针对大小数的处理类。
1.1 BigInteger
BigInteger 类型的数字范围较 Integer 类型的数字范围要大得多。 Integer 是 int 的包装类, int 的最大值是 2³¹-1 ,如果要计算更大的数字,使用 Integer 数据类型就无法实现了,所以 Java 中提供了 BigInteger 类来处理更大的数字。 BigInteger 支持任意精度的整数,也就是说在运算中 BigInteger 类型可以准确地表示任何大小的整数值而不会丢失任何信息。
在 BigInteger 类中封装了多种操作,除了基本的加、减、乘、除操作之外,还提供了绝对值、相反数、最大公约数以及判断是否为质数等操作。
使用 BigInteger 类,可以实例化一个 BigInteger 对象,并自动调用相应的构造函数。 BigInteger 类具有很多构造函数,但最直接的一种方式是参数以字符串形式代表要处理的数字。
语法如下 :
public BigInteger(String val)
其中 , val 是十进制字符串。
如果将 2 转换为 BigInteger 类型,可以使用一下语句进行初始化操作 :
BigInteger twoInstance = new BigInteger("2"); //将十进制 2 转换为 BigInteger 形式
参数 2 的双引号不能省略,因为参数是以字符串的形式存在的。
一旦创建了对象实例,就可以调用 BigInteger 类中的一些方法进行运算操作,包括基本的数学运算和位运算以及一些取相反数、取绝对值等操作。下面列举几种常用运算方法 :
( 1 ) publicBigInteger add(BigInteger val) : 做加法运算
(2 ) publicBigInteger subtract(BigInteger val) : 做减法运算
(3 ) publicBigInteger multiply(BigInteger val) : 做乘法运算
(4 ) publicBigInteger divide(BigInteger val) : 做除法运算
(5 ) publicBigInteger remainder(BigInteger val) : 做取余运算
(6 ) publicBigInteger[] divideAndRemainder (BigInteger val) : 用数组返回余数和商,结果数组中第一个值为商,第二个值为余数
(7 ) public BigInteger pow(intexponent) : 进行取参数的 exponent 次方操作
(8 ) publicBigInteger negate() : 取相反数
(9 ) public BigInteger shiftLeft(intn) : 将数字左移 n 位,如果 n 为负数,做右移操作
(10 )public BigInteger shiftRight(intn) : 将数字右移 n 位,如果 n 为负数,做左移操作
(11 )publicBigInteger and(BigInteger val) : 做与操作
(12 )publicBigInteger or(BigInteger val) : 做或操作
(13 )public intcompareTo(BigInteger val) : 做数字比较操作
(14 )public boolean equals(Object x) : 当参数 x 是 BigInteger 类型的数字并且数值相等时,返回 true(15 )publicBigInteger min(BigInteger val) : 返回较小的数值
(16 )public BigInteger max(BigInteger val) : 返回较大的数值
eg : 创建类,在类的主方法中创建 BigInteger 类的实例对象,调用该对象的各种方法实现大整数的加、减、乘、除和其他运算,并输出运算结果。
importjava.math.BigInteger;public classBigIntegerDemo {public static voidmain(String[] args) {
BigInteger bigInteger= new BigInteger("4");//实例化一个大数字
System.out.println("加法操作:" + bigInteger.add(new BigInteger("2")));
System.out.println("减法操作:" + bigInteger.subtract(new BigInteger("2")));
System.out.println("乘法操作:" + bigInteger.multiply(new BigInteger("2")));
System.out.println("除法操作:" + bigInteger.divide(new BigInteger("2")));
System.out.println("取商操作:" + bigInteger.divideAndRemainder(new BigInteger("3"))[0]);
System.out.println("取余数操作:" + bigInteger.divideAndRemainder(new BigInteger("3"))[1]);
System.out.println("做 2 次方操作:" + bigInteger.pow(2));
System.out.println("取相反数操作:" +bigInteger.negate());
}
}
运行结果为 :
加法操作:6减法操作:2乘法操作:8除法操作:2取商操作:1取余数操作:1做2 次方操作:16取相反数操作:-4
1.2 BigDecimal
BigDecimal 和 BigInteger 都能用来实现大数字的运算,不同的是 BigDecimal 加入了小数的概念。一般的 float 型和 double 型数据只可以用来做科学计算或工程计算,但由于在商业计算中要求数字精度比较高,所以要用到 java.math.BigDecimal 类。 BigDecimal 类支持任何精度的定点数,可以用它来精确计算货币值。
在 BigDecimal 类中常用的两个构造方法如下 :
public BigDecimal(double val) : 实例化时将双精度型转换为 BigDecimal 类型
public BigDecimal(String val) : 实例化时将字符串形式转换为 BigDecimal 类型
BigDecimal 类型的数字可以用来做超大的浮点数的运算,如加、减、乘、除等,但是在所有的运算中除法是最复杂的,因为在除不尽的情况下末位小数点的处理是需要考虑的。
下面列举 BigDecimal 类中实现加、减、乘、除的方法 :
publicBigDecimal add(BigDecimal augend) :做加法操作publicBigDecimal subtract(BigDecimal subtrahend) :做减法操作publicBigDecimal multiply(BigDecimal multiplicand) :做乘法操作public BigDecimal divide(BigDecimal divisor , int sacle ,int roundingMode) :做除法操作,方法中 3 个参数分别代表除数、商的小数点后的位数、近似处理模式
在上述方法中,BigDecimal 类中 divide() 方法有多种设置,用于返回商末位小数点的处理,这些模式的名称与含义如下 :
BigDecimal 类中 divide() 方法的多种处理模式
模式 含义
BigDecimal.ROUND_UP s商的最后一位如果大于 0 ,则向前进位,正负数都如此
BigDecimal .ROUND_DOWN 商的最后一位无论是什么数字都省略
BigDecimal .ROUND_CEILING
商如果是正数,按照 ROUND_UP 模式处理;
如果是负数,按照 ROUND_DOWN 模式处理。
这种模式的处理都会使近似值大于等于实际值。
BigDecimal .ROUND_FLOOR
与 ROUND_CEILING 模式相反,
商如果是正数,按照 ROUND_DOWN 模式处理;
如果是负数,按照ROUND_UP 模式处理。
这种模式的处理都会使近似值小于等于实际值。
BigDecimal .ROUND_HALF_DOWN
对商进行四舍五入操作,如果商最后一位小于等于 5,则做舍弃操作;
如果最后一位大于 5 ,则做进位操作,如 7.5 ≈ 7
BigDecimal .ROUND_HALF_UP
对商进行四舍五入操作,如果商的最后一位小于 5 则舍弃;
如果大于等于 5 ,进行进位操作,如 7.5 ≈ 8
BigDecimal ROUND_HALF_EVEN
如果商的倒数第二位为奇数,则按照 ROUND_HALF_UP 处理;
如果为偶数,则按照 ROUND_HALF_DOWN 处理,如 7.5 ≈ 8 , 8.5 ≈ 8
eg : 创建类,在类中分别定义 add() 、sub() 、mul() 和 div() 方法实现加、减、乘、除运算,并输出运算结果。
importjava.math.BigDecimal;public classBigDecimalDemo {static final int location = 10;/*** 定义加法方法,参数为加数与被加数
*@paramvalue1 相加的第一个数
*@paramvalue2 相加的第二个数
*@return两数之和*/
public BigDecimal add(double value1 , doublevalue2) {//实例化 Decimal 对象
BigDecimal b1 = newBigDecimal(Double.toString(value1));
BigDecimal b2= newBigDecimal(Double.toString(value2));return b1.add(b2); //调用加法方法
}/*** 定义减法方法,参数为减数与被减数
*@paramvalue1 被减数
*@paramvalue2 减数
*@return运算结果*/
public BigDecimal sub(double value1 , doublevalue2) {//实例化 Decimal 对象
BigDecimal b1 = newBigDecimal(Double.toString(value1));
BigDecimal b2= newBigDecimal(Double.toString(value2));return b1.subtract(b2); //调用减法方法
}/*** 定义乘法方法,参数为乘数与被乘数
*@paramvalue1 第一个乘数
*@paramvalue2 第二个乘数
*@return运算结果*/
public BigDecimal mul(double value1 , doublevalue2) {//实例化 Decimal 对象
BigDecimal b1 = newBigDecimal(Double.toString(value1));
BigDecimal b2= newBigDecimal(Double.toString(value2));return b1.multiply(b2); //调用乘法方法
}/*** 定义除法方法,参数为除数与被除数
*@paramvalue1 被除数
*@paramvalue2 除数
*@return运算结果*/
public BigDecimal div(double value1 , doublevalue2) {return div(value1, value2,location);//调用自定义除法方法
}public BigDecimal div(double value1 , double value2 ,intb) {if (b<0) {
System.out.println("b 值必须大于等于 0");
}
BigDecimal b1= newBigDecimal(Double.toString(value1));
BigDecimal b2= newBigDecimal(Double.toString(value2));//调用除法方法,商小数点保留 b 位,并将结果进行四舍五入操作
returnb1.divide(b2,b,BigDecimal.ROUND_HALF_UP);
}public static voidmain(String[] args) {
BigDecimalDemo b= newBigDecimalDemo();
System.out.println("两个数字相加结果:" + b.add(-7.5, 8.9));
System.out.println("两个数字相减结果:" + b.sub(-7.5, 8.9));
System.out.println("两个数字相乘结果:" + b.mul(-7.5, 8.9));
System.out.println("两个数字相除结果,结果小数后保留 10 位:" + b.div(10,2));
System.out.println("两个数字相除,保留小数后 5 位:" + b.div(-7.8,8.9,5));
}
}
运行结果为:
两个数字相加结果:1.4两个数字相减结果:-16.4两个数字相乘结果:-66.75两个数字相除结果,结果小数后保留10 位:5.0000000000两个数字相除,保留小数后5 位:-0.87640
原文:https://blog.csdn.net/js940814/article/details/80210456