标签:style blog http java color 使用
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);