Java的大数操作分为BigInteger和BigDecimal,但这两给类是分开使用的,有时候在编程的时候显得略微繁琐,现在编写了一个将二者合二为一的大数操作类。
大数操作类代码如下:
package blog; import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode; /**
*
* @author 瓦尔登湖畔的小木屋
* BigNumberOperation封装了对十进制大数整数和大数浮点数的四则运算的操作
*/
public class BigNumberOperation {
/**
* 对加法的封装操作
*/
public static BigInteger add(BigInteger number1, BigInteger number2) {
return number1.add(number2);
} public static BigDecimal add(BigDecimal number1, BigDecimal number2) {
return number1.add(number2);
} public static BigDecimal add(BigDecimal number1, BigInteger number2) {
return number1.add(new BigDecimal(number2));
} public static BigDecimal add(BigInteger number1, BigDecimal number2) {
return new BigDecimal(number1).add(number2);
} /**
* 对减法的封装操作
*/
public static BigInteger subtract(BigInteger number1, BigInteger number2) {
return number1.subtract(number2);
} public static BigDecimal subtract(BigDecimal number1, BigDecimal number2) {
return number1.subtract(number2);
} public static BigDecimal subtract(BigDecimal number1, BigInteger number2) {
return number1.subtract(new BigDecimal(number2));
} public static BigDecimal subtract(BigInteger number1, BigDecimal number2) {
return new BigDecimal(number1).subtract(number2);
} /**
* 对乘法的封装操作
*/
public static BigInteger multiply(BigInteger number1, BigInteger number2) {
return number1.multiply(number2);
} public static BigDecimal multiply(BigDecimal number1, BigDecimal number2) {
return number1.multiply(number2);
} public static BigDecimal multiply(BigDecimal number1, BigInteger number2) {
return number1.multiply(new BigDecimal(number2));
} public static BigDecimal multiply(BigInteger number1, BigDecimal number2) {
return new BigDecimal(number1).multiply(number2);
} /**
* 对除法的封装
* 大数浮点数时,结果默认保留10位小数,也可以通过count指定保留小数的位数
* 对于最末一位的取舍采取RoundingMode.HALF_EVEN模式
*/
public static BigInteger divide(BigInteger number1, BigInteger number2) {
return number1.divide(number2);
} public static BigDecimal divide(BigDecimal number1, BigDecimal number2) {
return number1.divide(number2, 10, RoundingMode.HALF_EVEN);
} public static BigDecimal divide(BigDecimal number1, BigDecimal number2, int count) {
return number1.divide(number2, count, RoundingMode.HALF_EVEN);
} public static BigDecimal divide(BigDecimal number1, BigInteger number2) {
return number1.divide(new BigDecimal(number2), 10, RoundingMode.HALF_EVEN);
} public static BigDecimal divide(BigDecimal number1, BigInteger number2, int count) {
return number1.divide(new BigDecimal(number2), count, RoundingMode.HALF_EVEN);
} public static BigDecimal divide(BigInteger number1, BigDecimal number2) {
return new BigDecimal(number1).divide(number2, 10, RoundingMode.HALF_EVEN);
} public static BigDecimal divide(BigInteger number1, BigDecimal number2, int count) {
return new BigDecimal(number1).divide(number2, count, RoundingMode.HALF_EVEN);
}
/*
* 由于当两个大数都是字符串时,无法通过重载实现,所以才返回Number,以同时适应BigInteger和BigDecimal
*/
public static Number add(String number1, String number2) {
if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) {
return new BigInteger(number1).add(new BigInteger(number2));
} else {
return new BigDecimal(number1).add(new BigDecimal(number2));
}
} public static Number subtract(String number1, String number2) {
if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) {
return new BigInteger(number1).subtract(new BigInteger(number2));
} else {
return new BigDecimal(number1).subtract(new BigDecimal(number2));
}
} public static Number multiply(String number1, String number2) {
if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) {
return new BigInteger(number1).multiply(new BigInteger(number2));
} else {
return new BigDecimal(number1).multiply(new BigDecimal(number2));
}
} public static Number divide(String number1, String number2) {
if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) {
return new BigInteger(number1).divide(new BigInteger(number2));
} else {
return new BigDecimal(number1).divide(new BigDecimal(number2), 10, RoundingMode.HALF_EVEN);
}
} public static BigDecimal divide(String number1, String number2, int count) {
return new BigDecimal(number1).divide(new BigDecimal(number2), count, RoundingMode.HALF_EVEN);
}
}
测试代码如下:
package blog; import java.math.BigDecimal;
import java.math.BigInteger; public class Main {
public static void main(String[] args) {
String str1 = "46556545";
String str2 = "45454545.45454544"; BigInteger num1 = new BigInteger(str1);
BigDecimal num2 = new BigDecimal(str2);
System.out.println("当两个参数是大数时的测试:");
System.out.println("大数加法的测试:");
System.out.println(num1 + " + " + num1 + " = " + BigNumberOperation.add(num1, num1));
System.out.println(num1 + " + " + num2 + " = " + BigNumberOperation.add(num1, num2));
System.out.println(num2 + " + " + num1 + " = " + BigNumberOperation.add(num2, num1));
System.out.println(num2 + " + " + num2 + " = " + BigNumberOperation.add(num2, num2));
System.out.println(); System.out.println("大数减法的测试:");
System.out.println(num1 + " - " + num1 + " = " + BigNumberOperation.subtract(num1, num1));
System.out.println(num1 + " - " + num2 + " = " + BigNumberOperation.subtract(num1, num2));
System.out.println(num2 + " - " + num1 + " = " + BigNumberOperation.subtract(num2, num1));
System.out.println(num2 + " - " + num2 + " = " + BigNumberOperation.subtract(num2, num2));
System.out.println(); System.out.println("大数乘法的测试:");
System.out.println(num1 + " * " + num1 + " = " + BigNumberOperation.multiply(num1, num1));
System.out.println(num1 + " * " + num2 + " = " + BigNumberOperation.multiply(num1, num2));
System.out.println(num2 + " * " + num1 + " = " + BigNumberOperation.multiply(num2, num1));
System.out.println(num2 + " * " + num2 + " = " + BigNumberOperation.multiply(num2, num2));
System.out.println(); System.out.println("大数除法的测试:");
System.out.println(num1 + " / " + num1 + " = " + BigNumberOperation.divide(num1, num1));
System.out.println(num1 + " / " + num2 + " = " + BigNumberOperation.divide(num1, num2));
System.out.println(num2 + " / " + num1 + " = " + BigNumberOperation.divide(num2, num1));
System.out.println(num2 + " / " + num2 + " = " + BigNumberOperation.divide(num2, num2));
System.out.println("大数除法保留五位小数:");
System.out.println(num1 + " / " + num2 + " = " + BigNumberOperation.divide(num1, num2, 5));
System.out.println(num2 + " / " + num1 + " = " + BigNumberOperation.divide(num2, num1, 5));
System.out.println(num2 + " / " + num2 + " = " + BigNumberOperation.divide(num2, num2, 5));
System.out.println(""); System.out.println("###############################################");
System.out.println("当两个参数为字符串是的测试:");
System.out.println("加法测试:");
System.out.println(str1 + " + " + str1 + " = " + BigNumberOperation.add(str1, str1));
System.out.println(str1 + " + " + str2 + " = " + BigNumberOperation.add(str1, str2));
System.out.println(str2 + " + " + str1 + " = " + BigNumberOperation.add(str2, str1));
System.out.println(str2 + " + " + str2 + " = " + BigNumberOperation.add(str2, str2));
System.out.println();
System.out.println("减法测试:");
System.out.println(str1 + " - " + str1 + " = " + BigNumberOperation.subtract(str1, str1));
System.out.println(str1 + " - " + str2 + " = " + BigNumberOperation.subtract(str1, str2));
System.out.println(str2 + " - " + str1 + " = " + BigNumberOperation.subtract(str2, str1));
System.out.println(str2 + " - " + str2 + " = " + BigNumberOperation.subtract(str2, str2));
System.out.println();
System.out.println("乘法测试:");
System.out.println(str1 + " * " + str1 + " = " + BigNumberOperation.multiply(str1, str1));
System.out.println(str1 + " * " + str2 + " = " + BigNumberOperation.multiply(str1, str2));
System.out.println(str2 + " * " + str1 + " = " + BigNumberOperation.multiply(str2, str1));
System.out.println(str2 + " * " + str2 + " = " + BigNumberOperation.multiply(str2, str2));
System.out.println();
System.out.println("除法测试:");
System.out.println(str1 + " / " + str1 + " = " + BigNumberOperation.divide(str1, str1));
System.out.println(str1 + " / " + str2 + " = " + BigNumberOperation.divide(str1, str2));
System.out.println(str2 + " / " + str1 + " = " + BigNumberOperation.divide(str2, str1));
System.out.println(str2 + " / " + str2 + " = " + BigNumberOperation.divide(str2, str2));
System.out.println();
System.out.println("除法测试:");
System.out.println(str1 + " / " + str1 + " = " + BigNumberOperation.divide(str1, str1, 5));
System.out.println(str1 + " / " + str2 + " = " + BigNumberOperation.divide(str1, str2, 5));
System.out.println(str2 + " / " + str1 + " = " + BigNumberOperation.divide(str2, str1, 5));
System.out.println(str2 + " / " + str2 + " = " + BigNumberOperation.divide(str2, str2, 5));
}
}
测试结果如下:
当两个参数是大数时的测试:
大数加法的测试:
46556545 + 46556545 = 93113090
46556545 + 45454545.45454544 = 92011090.45454544
45454545.45454544 + 46556545 = 92011090.45454544
45454545.45454544 + 45454545.45454544 = 90909090.90909088
大数减法的测试:
46556545 - 46556545 = 0
46556545 - 45454545.45454544 = 1101999.54545456
45454545.45454544 - 46556545 = -1101999.54545456
45454545.45454544 - 45454545.45454544 = 0E-8
大数乘法的测试:
46556545 * 46556545 = 2167511882337025
46556545 * 45454545.45454544 = 2116206590909090.23190480
45454545.45454544 * 46556545 = 2116206590909090.23190480
45454545.45454544 * 45454545.45454544 = 2066115702479337.5206611570247936
大数除法的测试:
46556545 / 46556545 = 1
46556545 / 45454545.45454544 = 1.0242439900
45454545.45454544 / 46556545 = 0.9763298684
45454545.45454544 / 45454545.45454544 = 1.0000000000
大数除法保留五位小数:
46556545 / 45454545.45454544 = 1.02424
45454545.45454544 / 46556545 = 0.97633
45454545.45454544 / 45454545.45454544 = 1.00000
###############################################
当两个参数为字符串是的测试:
加法测试:
46556545 + 46556545 = 93113090
46556545 + 45454545.45454544 = 92011090.45454544
45454545.45454544 + 46556545 = 92011090.45454544
45454545.45454544 + 45454545.45454544 = 90909090.90909088
减法测试:
46556545 - 46556545 = 0
46556545 - 45454545.45454544 = 1101999.54545456
45454545.45454544 - 46556545 = -1101999.54545456
45454545.45454544 - 45454545.45454544 = 0E-8
乘法测试:
46556545 * 46556545 = 2167511882337025
46556545 * 45454545.45454544 = 2116206590909090.23190480
45454545.45454544 * 46556545 = 2116206590909090.23190480
45454545.45454544 * 45454545.45454544 = 2066115702479337.5206611570247936
除法测试:
46556545 / 46556545 = 1
46556545 / 45454545.45454544 = 1.0242439900
45454545.45454544 / 46556545 = 0.9763298684
45454545.45454544 / 45454545.45454544 = 1.0000000000
除法测试:
46556545 / 46556545 = 1.00000
46556545 / 45454545.45454544 = 1.02424
45454545.45454544 / 46556545 = 0.97633
45454545.45454544 / 45454545.45454544 = 1.00000