Java的大数操作分为BigInteger和BigDecimal,但这两给类是分开使用的,有时候在编程的时候显得略微繁琐,现在编写了一个将二者合二为一的大数操作类。
大数操作类代码如下:
1 package blog; 2 3 import java.math.BigDecimal; 4 import java.math.BigInteger; 5 import java.math.RoundingMode; 6 7 /** 8 * 9 * @author 瓦尔登湖畔的小木屋 10 * BigNumberOperation封装了对十进制大数整数和大数浮点数的四则运算的操作 11 */ 12 public class BigNumberOperation { 13 /** 14 * 对加法的封装操作 15 */ 16 public static BigInteger add(BigInteger number1, BigInteger number2) { 17 return number1.add(number2); 18 } 19 20 public static BigDecimal add(BigDecimal number1, BigDecimal number2) { 21 return number1.add(number2); 22 } 23 24 public static BigDecimal add(BigDecimal number1, BigInteger number2) { 25 return number1.add(new BigDecimal(number2)); 26 } 27 28 public static BigDecimal add(BigInteger number1, BigDecimal number2) { 29 return new BigDecimal(number1).add(number2); 30 } 31 32 /** 33 * 对减法的封装操作 34 */ 35 public static BigInteger subtract(BigInteger number1, BigInteger number2) { 36 return number1.subtract(number2); 37 } 38 39 public static BigDecimal subtract(BigDecimal number1, BigDecimal number2) { 40 return number1.subtract(number2); 41 } 42 43 public static BigDecimal subtract(BigDecimal number1, BigInteger number2) { 44 return number1.subtract(new BigDecimal(number2)); 45 } 46 47 public static BigDecimal subtract(BigInteger number1, BigDecimal number2) { 48 return new BigDecimal(number1).subtract(number2); 49 } 50 51 /** 52 * 对乘法的封装操作 53 */ 54 public static BigInteger multiply(BigInteger number1, BigInteger number2) { 55 return number1.multiply(number2); 56 } 57 58 public static BigDecimal multiply(BigDecimal number1, BigDecimal number2) { 59 return number1.multiply(number2); 60 } 61 62 public static BigDecimal multiply(BigDecimal number1, BigInteger number2) { 63 return number1.multiply(new BigDecimal(number2)); 64 } 65 66 public static BigDecimal multiply(BigInteger number1, BigDecimal number2) { 67 return new BigDecimal(number1).multiply(number2); 68 } 69 70 /** 71 * 对除法的封装 72 * 大数浮点数时,结果默认保留10位小数,也可以通过count指定保留小数的位数 73 * 对于最末一位的取舍采取RoundingMode.HALF_EVEN模式 74 */ 75 public static BigInteger divide(BigInteger number1, BigInteger number2) { 76 return number1.divide(number2); 77 } 78 79 public static BigDecimal divide(BigDecimal number1, BigDecimal number2) { 80 return number1.divide(number2, 10, RoundingMode.HALF_EVEN); 81 } 82 83 public static BigDecimal divide(BigDecimal number1, BigDecimal number2, int count) { 84 return number1.divide(number2, count, RoundingMode.HALF_EVEN); 85 } 86 87 public static BigDecimal divide(BigDecimal number1, BigInteger number2) { 88 return number1.divide(new BigDecimal(number2), 10, RoundingMode.HALF_EVEN); 89 } 90 91 public static BigDecimal divide(BigDecimal number1, BigInteger number2, int count) { 92 return number1.divide(new BigDecimal(number2), count, RoundingMode.HALF_EVEN); 93 } 94 95 public static BigDecimal divide(BigInteger number1, BigDecimal number2) { 96 return new BigDecimal(number1).divide(number2, 10, RoundingMode.HALF_EVEN); 97 } 98 99 public static BigDecimal divide(BigInteger number1, BigDecimal number2, int count) { 100 return new BigDecimal(number1).divide(number2, count, RoundingMode.HALF_EVEN); 101 } 102 /* 103 * 由于当两个大数都是字符串时,无法通过重载实现,所以才返回Number,以同时适应BigInteger和BigDecimal 104 */ 105 public static Number add(String number1, String number2) { 106 if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) { 107 return new BigInteger(number1).add(new BigInteger(number2)); 108 } else { 109 return new BigDecimal(number1).add(new BigDecimal(number2)); 110 } 111 } 112 113 public static Number subtract(String number1, String number2) { 114 if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) { 115 return new BigInteger(number1).subtract(new BigInteger(number2)); 116 } else { 117 return new BigDecimal(number1).subtract(new BigDecimal(number2)); 118 } 119 } 120 121 public static Number multiply(String number1, String number2) { 122 if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) { 123 return new BigInteger(number1).multiply(new BigInteger(number2)); 124 } else { 125 return new BigDecimal(number1).multiply(new BigDecimal(number2)); 126 } 127 } 128 129 public static Number divide(String number1, String number2) { 130 if (number1.matches("[0-9]+") && number2.matches("[0-9]+")) { 131 return new BigInteger(number1).divide(new BigInteger(number2)); 132 } else { 133 return new BigDecimal(number1).divide(new BigDecimal(number2), 10, RoundingMode.HALF_EVEN); 134 } 135 } 136 137 public static BigDecimal divide(String number1, String number2, int count) { 138 return new BigDecimal(number1).divide(new BigDecimal(number2), count, RoundingMode.HALF_EVEN); 139 } 140 }
测试代码如下:
1 package blog; 2 3 import java.math.BigDecimal; 4 import java.math.BigInteger; 5 6 public class Main { 7 public static void main(String[] args) { 8 String str1 = "46556545"; 9 String str2 = "45454545.45454544"; 10 11 BigInteger num1 = new BigInteger(str1); 12 BigDecimal num2 = new BigDecimal(str2); 13 System.out.println("当两个参数是大数时的测试:"); 14 System.out.println("大数加法的测试:"); 15 System.out.println(num1 + " + " + num1 + " = " + BigNumberOperation.add(num1, num1)); 16 System.out.println(num1 + " + " + num2 + " = " + BigNumberOperation.add(num1, num2)); 17 System.out.println(num2 + " + " + num1 + " = " + BigNumberOperation.add(num2, num1)); 18 System.out.println(num2 + " + " + num2 + " = " + BigNumberOperation.add(num2, num2)); 19 System.out.println(); 20 21 System.out.println("大数减法的测试:"); 22 System.out.println(num1 + " - " + num1 + " = " + BigNumberOperation.subtract(num1, num1)); 23 System.out.println(num1 + " - " + num2 + " = " + BigNumberOperation.subtract(num1, num2)); 24 System.out.println(num2 + " - " + num1 + " = " + BigNumberOperation.subtract(num2, num1)); 25 System.out.println(num2 + " - " + num2 + " = " + BigNumberOperation.subtract(num2, num2)); 26 System.out.println(); 27 28 System.out.println("大数乘法的测试:"); 29 System.out.println(num1 + " * " + num1 + " = " + BigNumberOperation.multiply(num1, num1)); 30 System.out.println(num1 + " * " + num2 + " = " + BigNumberOperation.multiply(num1, num2)); 31 System.out.println(num2 + " * " + num1 + " = " + BigNumberOperation.multiply(num2, num1)); 32 System.out.println(num2 + " * " + num2 + " = " + BigNumberOperation.multiply(num2, num2)); 33 System.out.println(); 34 35 System.out.println("大数除法的测试:"); 36 System.out.println(num1 + " / " + num1 + " = " + BigNumberOperation.divide(num1, num1)); 37 System.out.println(num1 + " / " + num2 + " = " + BigNumberOperation.divide(num1, num2)); 38 System.out.println(num2 + " / " + num1 + " = " + BigNumberOperation.divide(num2, num1)); 39 System.out.println(num2 + " / " + num2 + " = " + BigNumberOperation.divide(num2, num2)); 40 System.out.println("大数除法保留五位小数:"); 41 System.out.println(num1 + " / " + num2 + " = " + BigNumberOperation.divide(num1, num2, 5)); 42 System.out.println(num2 + " / " + num1 + " = " + BigNumberOperation.divide(num2, num1, 5)); 43 System.out.println(num2 + " / " + num2 + " = " + BigNumberOperation.divide(num2, num2, 5)); 44 System.out.println(""); 45 46 System.out.println("###############################################"); 47 System.out.println("当两个参数为字符串是的测试:"); 48 System.out.println("加法测试:"); 49 System.out.println(str1 + " + " + str1 + " = " + BigNumberOperation.add(str1, str1)); 50 System.out.println(str1 + " + " + str2 + " = " + BigNumberOperation.add(str1, str2)); 51 System.out.println(str2 + " + " + str1 + " = " + BigNumberOperation.add(str2, str1)); 52 System.out.println(str2 + " + " + str2 + " = " + BigNumberOperation.add(str2, str2)); 53 System.out.println(); 54 System.out.println("减法测试:"); 55 System.out.println(str1 + " - " + str1 + " = " + BigNumberOperation.subtract(str1, str1)); 56 System.out.println(str1 + " - " + str2 + " = " + BigNumberOperation.subtract(str1, str2)); 57 System.out.println(str2 + " - " + str1 + " = " + BigNumberOperation.subtract(str2, str1)); 58 System.out.println(str2 + " - " + str2 + " = " + BigNumberOperation.subtract(str2, str2)); 59 System.out.println(); 60 System.out.println("乘法测试:"); 61 System.out.println(str1 + " * " + str1 + " = " + BigNumberOperation.multiply(str1, str1)); 62 System.out.println(str1 + " * " + str2 + " = " + BigNumberOperation.multiply(str1, str2)); 63 System.out.println(str2 + " * " + str1 + " = " + BigNumberOperation.multiply(str2, str1)); 64 System.out.println(str2 + " * " + str2 + " = " + BigNumberOperation.multiply(str2, str2)); 65 System.out.println(); 66 System.out.println("除法测试:"); 67 System.out.println(str1 + " / " + str1 + " = " + BigNumberOperation.divide(str1, str1)); 68 System.out.println(str1 + " / " + str2 + " = " + BigNumberOperation.divide(str1, str2)); 69 System.out.println(str2 + " / " + str1 + " = " + BigNumberOperation.divide(str2, str1)); 70 System.out.println(str2 + " / " + str2 + " = " + BigNumberOperation.divide(str2, str2)); 71 System.out.println(); 72 System.out.println("除法测试:"); 73 System.out.println(str1 + " / " + str1 + " = " + BigNumberOperation.divide(str1, str1, 5)); 74 System.out.println(str1 + " / " + str2 + " = " + BigNumberOperation.divide(str1, str2, 5)); 75 System.out.println(str2 + " / " + str1 + " = " + BigNumberOperation.divide(str2, str1, 5)); 76 System.out.println(str2 + " / " + str2 + " = " + BigNumberOperation.divide(str2, str2, 5)); 77 } 78 }
测试结果如下:
当两个参数是大数时的测试:
大数加法的测试:
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
Java的大数操作分为BigInteger和BigDecimal