处理大数字BigInteger与BigDecimal

时间:2023-02-11 11:02:38

Edit

 

处理大数字BigInteger与BigDecimal

关键技术剖析:

BigInteger:支持任意精度的整数,可以精确地表示任意大小的整数值,同时在运算过程中不会丢失任何信息。

BigDecimal:可以精确地表示任意精度的小数,同时在运算过程中不会丢失任何信息。

源码:

1.import java.math.BigDecimal;
2.
3.import java.math.BigInteger;
4.
5.
6.
7.
8.
9.public class BigNumber {
10.
11.
12.
13.//默认除法运算精度,即保留小数点多少位
14.
15.private static final int DEFAULT_DIV_SCALE = 10;
16.
17.
18.
19.//这个类不能实例化
20.
21.private BigNumber() {
22.
23.}
24.
25.
26.
27.
28.
29.public static double add(double v1, double v2) {
30.
31.BigDecimal b1 = new BigDecimal(Double.toString(v1));
32.
33.BigDecimal b2 = new BigDecimal(Double.toString(v2));
34.
35.return (b1.add(b2)).doubleValue();
36.
37.}
38.
39.
40.
41.
42.
43.public static double sub(double v1, double v2) {
44.
45.BigDecimal b1 = new BigDecimal(Double.toString(v1));
46.
47.BigDecimal b2 = new BigDecimal(Double.toString(v2));
48.
49.return (b1.subtract(b2)).doubleValue();
50.
51.}
52.
53.
54.
55.
56.
57.public static double mul(double v1, double v2) {
58.
59.BigDecimal b1 = new BigDecimal(Double.toString(v1));
60.
61.BigDecimal b2 = new BigDecimal(Double.toString(v2));
62.
63.return (b1.multiply(b2)).doubleValue();
64.
65.}
66.
67.
68.
69.
70.
71.public static double div(double v1, double v2) {
72.
73.return div(v1, v2, DEFAULT_DIV_SCALE);
74.
75.}
76.
77.
78.
79.
80.
81.public static double div(double v1, double v2, int scale) {
82.
83.if (scale < 0) {
84.
85.System.err.println("除法精度必须大于0!");
86.
87.return 0;
88.
89.}
90.
91.BigDecimal b1 = new BigDecimal(Double.toString(v1));
92.
93.BigDecimal b2 = new BigDecimal(Double.toString(v2));
94.
95.return (b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP)).doubleValue();
96.
97.}
98.
99.
100.
101.
102.
103.public static BigInteger getFactorial(int n) {
104.
105.if (n < 0) {
106.
107.System.err.println("n必须大于等于0!");
108.
109.return new BigInteger("-1");
110.
111.} else if (n == 0) {
112.
113.return new BigInteger("0");
114.
115.}
116.
117.//将数组换成字符串后构造BigInteger
118.
119.BigInteger result = new BigInteger("1");
120.
121.for (; n > 0; n--) {
122.
123.//将数字n转换成字符串后,再构造一个BigInteger对象,与现有结果做乘法
124.
125.result = result.multiply(new BigInteger(new Integer(n).toString()));
126.
127.}
128.
129.return result;
130.
131.}
132.
133.
134.
135.public static void main(String[] args) {
136.
137.
138.
139.// 如果我们编译运行下面这个程序会看到什么?
140.
141.System.out.println(0.05 + 0.01);
142.
143.System.out.println(1.0 - 0.42);
144.
145.System.out.println(4.015 * 100);
146.
147.System.out.println(123.3 / 100);
148.
149.// 0.060000000000000005
150.
151.// 0.5800000000000001
152.
153.// 401.49999999999994
154.
155.// 1.2329999999999999
156.
157.
158.
159.//计算阶乘,可以将n设得更大
160.
161.int n = 30;
162.
163.System.out.println("计算n的阶乘" + n + "! = " + BigNumber.getFactorial(n));
164.
165.
166.
167.//用double构造BigDecimal
168.
169.BigDecimal bd1 = new BigDecimal(0.1);
170.
171.System.out.println("(bd1 = new BigDecimal(0.1)) = " + bd1.toString());
172.
173.//用String构造BigDecimal
174.
175.BigDecimal bd2 = new BigDecimal("0.1");
176.
177.System.out.println("(bd2 = new BigDecimal("0.1")) = "
178.
179.+ bd2.toString());
180.
181.
182.
183.BigDecimal bd3 = new BigDecimal("0.10");
184.
185.//equals方法比较两个BigDecimal对象是否相等,相等返回true,不等返回false
186.
187.System.out.println("bd2.equals(bd3) = " + bd2.equals(bd3));//false
188.
189.//compareTo方法比较两个BigDecimal对象的大小,相等返回0,小于返回-1,大于返回1。
190.
191.System.out.println("bd2.compareTo(bd3) = " + bd2.compareTo(bd3));//0
192.
193.
194.
195.//进行精确计算
196.
197.System.out.println("0.05 + 0.01 = " + BigNumber.add(0.05, 0.01));
198.
199.System.out.println("1.0 - 0.42 = " + BigNumber.add(1.0, 0.42));
200.
201.System.out.println("4.015 * 100 =" + BigNumber.add(4.015, 100));
202.
203.System.out.println("123.3 / 100 = " + BigNumber.add(123.3, 100));
204.
205.}
206.
207.}

运行结果:

0.060000000000000005
0.5800000000000001
401.49999999999994
1.2329999999999999
计算n的阶乘30! = 265252859812191058636308480000000
(bd1 = new BigDecimal(0.1)) = 0.1000000000000000055511151231257827021181583404541015625
(bd2 = new BigDecimal(“0.1”)) = 0.1
bd2.equals(bd3) = false
bd2.compareTo(bd3) = 0
0.05 + 0.01 = 0.06
1.0 - 0.42 = 1.42
4.015 * 100 =104.015
123.3 / 100 = 223.3

源码分析:

BigInteger与BigDecimal都是不可变的(immutable)的,在进行每一步运算时,都会产生一个新的对象,由于创建对象会引起开销,因此它们不适合于大量的数学运算,应尽量使用long、float、double等基本类型做科学计算或者工程计算。设计BigInteger与BigDecimal的目的是用来精确地表示大整数和小数,常用于商业计算中。

BigDecimal够造方法的参数类型有4种,其中的两个用BigInteger构造,另一个是用double构造,还有一个使用String构造。应该避免使用double构造BigDecimal,因为:有些数字用double根本无法精确表示,传给BigDecimal构造方法时就已经不精确了。比如,new BigDecimal(0.1)得到的值是0.1000000000000000055511151231257827021181583404541015625。使用new BigDecimal("0.1")得到的值是0.1。因此,如果需要精确计算,用String构造BigDecimal,避免用double构造,尽管它看起来更简单!


  • equals()方法认为0.1和0.1是相等的,返回true,而认为0.10和0.1是不等的,结果返回false。方法compareTo()则认为0.1与0.1相等,0.10与0.1也相等。所以在从数值上比较两个BigDecimal值时,应该使用compareTo()而不是 equals()

  • 另外还有一些情形,任意精度的小数运算仍不能表示精确结果。例如,1除以9会产生无限循环的小数 .111111…。出于这个原因,在进行除法运算时,BigDecimal可以让您显式地控制舍入

常用方法

1.valueOf(parament); 将参数转换为制定的类型
比如

1.int a=3;
2.BigInteger b=BigInteger.valueOf(a);
3.
b=3;
4.
String s=”12345;
5.BigInteger c=BigInteger.valueOf(s);
6.则c=12345

2.add(); 大整数相加

BigInteger a=new BigInteger(“23”);

BigInteger b=new BigInteger(“34”);

a.add(b);


3.subtract(); 相减

4.multiply(); 相乘

5.divide(); 相除取整

6.remainder();取余

7.pow(); a.pow(b)=a^b

8.gcd(); 最大公约数

9.abs(); 绝对值

10.negate();取反数

11.mod(); a.mod(b)=a%b=a.remainder(b);

12.max(); min();

13.punlic int comareTo();

14.boolean equals(); 是否相等


BigInteger构造函数:

一般用到以下两种:

BigInteger(String val);

将指定字符串转换为十进制表示形式;

BigInteger(String val,int radix);

将指定基数的BigInteger的字符串表示形式转换为BigInteger


基本常量:

A=BigInteger.ONE 1

B=BigInteger.TEN 10

C=BigInteger.ZERO 0

@%2802.JavaSE%29%5BBigInteger%2CBigDecimal%5D%0A%23%u5904%u7406%u5927%u6570%u5B57BigInteger%u4E0EBigDecimal%20%0A%0A**%u5173%u952E%u6280%u672F%u5256%u6790%uFF1A**%0A%0ABigInteger%uFF1A%u652F%u6301%u4EFB%u610F%u7CBE%u5EA6%u7684%u6574%u6570%uFF0C%u53EF%u4EE5%u7CBE%u786E%u5730%u8868%u793A%u4EFB%u610F%u5927%u5C0F%u7684%u6574%u6570%u503C%uFF0C%u540C%u65F6%u5728%u8FD0%u7B97%u8FC7%u7A0B%u4E2D%u4E0D%u4F1A%u4E22%u5931%u4EFB%u4F55%u4FE1%u606F%u3002%0A%0ABigDecimal%uFF1A%u53EF%u4EE5%u7CBE%u786E%u5730%u8868%u793A%u4EFB%u610F%u7CBE%u5EA6%u7684%u5C0F%u6570%uFF0C%u540C%u65F6%u5728%u8FD0%u7B97%u8FC7%u7A0B%u4E2D%u4E0D%u4F1A%u4E22%u5931%u4EFB%u4F55%u4FE1%u606F%u3002%0A%0A%20%0A%0A**%u6E90%u7801%uFF1A**%0A%60%60%60%0Aimport%20java.math.BigDecimal%3B%0A%0Aimport%20java.math.BigInteger%3B%0A%0A%20%0A%0A%20%0A%0Apublic%20class%20BigNumber%20%7B%0A%0A%20%0A%0A//%u9ED8%u8BA4%u9664%u6CD5%u8FD0%u7B97%u7CBE%u5EA6%2C%u5373%u4FDD%u7559%u5C0F%u6570%u70B9%u591A%u5C11%u4F4D%20%0A%0Aprivate%20static%20final%20int%20DEFAULT_DIV_SCALE%20%3D%2010%3B%0A%0A%20%0A%0A//%u8FD9%u4E2A%u7C7B%u4E0D%u80FD%u5B9E%u4F8B%u5316%20%0A%0Aprivate%20BigNumber%28%29%20%7B%0A%0A%7D%0A%0A%20%0A%0A%20%0A%0Apublic%20static%20double%20add%28double%20v1%2C%20double%20v2%29%20%7B%0A%0ABigDecimal%20b1%20%3D%20new%20BigDecimal%28Double.toString%28v1%29%29%3B%0A%0ABigDecimal%20b2%20%3D%20new%20BigDecimal%28Double.toString%28v2%29%29%3B%0A%0Areturn%20%28b1.add%28b2%29%29.doubleValue%28%29%3B%0A%0A%7D%0A%0A%20%0A%0A%20%0A%0Apublic%20static%20double%20sub%28double%20v1%2C%20double%20v2%29%20%7B%0A%0ABigDecimal%20b1%20%3D%20new%20BigDecimal%28Double.toString%28v1%29%29%3B%0A%0ABigDecimal%20b2%20%3D%20new%20BigDecimal%28Double.toString%28v2%29%29%3B%0A%0Areturn%20%28b1.subtract%28b2%29%29.doubleValue%28%29%3B%0A%0A%7D%0A%0A%20%0A%0A%20%0A%0Apublic%20static%20double%20mul%28double%20v1%2C%20double%20v2%29%20%7B%0A%0ABigDecimal%20b1%20%3D%20new%20BigDecimal%28Double.toString%28v1%29%29%3B%0A%0ABigDecimal%20b2%20%3D%20new%20BigDecimal%28Double.toString%28v2%29%29%3B%0A%0Areturn%20%28b1.multiply%28b2%29%29.doubleValue%28%29%3B%0A%0A%7D%0A%0A%20%0A%0A%20%0A%0Apublic%20static%20double%20div%28double%20v1%2C%20double%20v2%29%20%7B%0A%0Areturn%20div%28v1%2C%20v2%2C%20DEFAULT_DIV_SCALE%29%3B%0A%0A%7D%0A%0A%20%0A%0A%20%0A%0Apublic%20static%20double%20div%28double%20v1%2C%20double%20v2%2C%20int%20scale%29%20%7B%0A%0Aif%20%28scale%20%3C%200%29%20%7B%0A%0ASystem.err.println%28%22%u9664%u6CD5%u7CBE%u5EA6%u5FC5%u987B%u5927%u4E8E0%21%22%29%3B%0A%0Areturn%200%3B%0A%0A%7D%0A%0ABigDecimal%20b1%20%3D%20new%20BigDecimal%28Double.toString%28v1%29%29%3B%0A%0ABigDecimal%20b2%20%3D%20new%20BigDecimal%28Double.toString%28v2%29%29%3B%0A%0Areturn%20%28b1.divide%28b2%2C%20scale%2C%20BigDecimal.ROUND_HALF_UP%29%29.doubleValue%28%29%3B%0A%0A%7D%0A%0A%20%0A%0A%20%0A%0Apublic%20static%20BigInteger%20getFactorial%28int%20n%29%20%7B%0A%0Aif%20%28n%20%3C%200%29%20%7B%0A%0ASystem.err.println%28%22n%u5FC5%u987B%u5927%u4E8E%u7B49%u4E8E0%uFF01%22%29%3B%0A%0Areturn%20new%20BigInteger%28%22-1%22%29%3B%0A%0A%7D%20else%20if%20%28n%20%3D%3D%200%29%20%7B%0A%0Areturn%20new%20BigInteger%28%220%22%29%3B%0A%0A%7D%0A%0A//%u5C06%u6570%u7EC4%u6362%u6210%u5B57%u7B26%u4E32%u540E%u6784%u9020BigInteger%0A%0ABigInteger%20result%20%3D%20new%20BigInteger%28%221%22%29%3B%0A%0Afor%20%28%3B%20n%20%3E%200%3B%20n--%29%20%7B%0A%0A//%u5C06%u6570%u5B57n%u8F6C%u6362%u6210%u5B57%u7B26%u4E32%u540E%uFF0C%u518D%u6784%u9020%u4E00%u4E2ABigInteger%u5BF9%u8C61%uFF0C%u4E0E%u73B0%u6709%u7ED3%u679C%u505A%u4E58%u6CD5%0A%0Aresult%20%3D%20result.multiply%28new%20BigInteger%28new%20Integer%28n%29.toString%28%29%29%29%3B%0A%0A%7D%0A%0Areturn%20result%3B%0A%0A%7D%0A%0A%20%0A%0Apublic%20static%20void%20main%28String%5B%5D%20args%29%20%7B%0A%0A%20%0A%0A//%20%u5982%u679C%u6211%u4EEC%u7F16%u8BD1%u8FD0%u884C%u4E0B%u9762%u8FD9%u4E2A%u7A0B%u5E8F%u4F1A%u770B%u5230%u4EC0%u4E48%uFF1F%0A%0ASystem.out.println%280.05%20+%200.01%29%3B%0A%0ASystem.out.println%281.0%20-%200.42%29%3B%0A%0ASystem.out.println%284.015%20*%20100%29%3B%0A%0ASystem.out.println%28123.3%20/%20100%29%3B%0A%0A//%200.060000000000000005%0A%0A//%200.5800000000000001%0A%0A//%20401.49999999999994%0A%0A//%201.2329999999999999%0A%0A%20%0A%0A//%u8BA1%u7B97%u9636%u4E58%uFF0C%u53EF%u4EE5%u5C06n%u8BBE%u5F97%u66F4%u5927%0A%0Aint%20n%20%3D%2030%3B%0A%0ASystem.out.println%28%22%u8BA1%u7B97n%u7684%u9636%u4E58%22%20+%20n%20+%20%22%21%20%3D%20%22%20+%20BigNumber.getFactorial%28n%29%29%3B%0A%0A%20%0A%0A//%u7528double%u6784%u9020BigDecimal%0A%0ABigDecimal%20bd1%20%3D%20new%20BigDecimal%280.1%29%3B%0A%0ASystem.out.println%28%22%28bd1%20%3D%20new%20BigDecimal%280.1%29%29%20%3D%20%22%20+%20bd1.toString%28%29%29%3B%0A%0A//%u7528String%u6784%u9020BigDecimal%0A%0ABigDecimal%20bd2%20%3D%20new%20BigDecimal%28%220.1%22%29%3B%0A%0ASystem.out.println%28%22%28bd2%20%3D%20new%20BigDecimal%28%220.1%22%29%29%20%3D%20%22%0A%0A+%20bd2.toString%28%29%29%3B%0A%0A%20%0A%0ABigDecimal%20bd3%20%3D%20new%20BigDecimal%28%220.10%22%29%3B%0A%0A//equals%u65B9%u6CD5%u6BD4%u8F83%u4E24%u4E2ABigDecimal%u5BF9%u8C61%u662F%u5426%u76F8%u7B49%uFF0C%u76F8%u7B49%u8FD4%u56DEtrue%uFF0C%u4E0D%u7B49%u8FD4%u56DEfalse%0A%0ASystem.out.println%28%22bd2.equals%28bd3%29%20%3D%20%22%20+%20bd2.equals%28bd3%29%29%3B//false%0A%0A//compareTo%u65B9%u6CD5%u6BD4%u8F83%u4E24%u4E2ABigDecimal%u5BF9%u8C61%u7684%u5927%u5C0F%uFF0C%u76F8%u7B49%u8FD4%u56DE0%uFF0C%u5C0F%u4E8E%u8FD4%u56DE-1%uFF0C%u5927%u4E8E%u8FD4%u56DE1%u3002%0A%0ASystem.out.println%28%22bd2.compareTo%28bd3%29%20%3D%20%22%20+%20bd2.compareTo%28bd3%29%29%3B//0%0A%0A%20%0A%0A//%u8FDB%u884C%u7CBE%u786E%u8BA1%u7B97%0A%0ASystem.out.println%28%220.05%20+%200.01%20%3D%20%22%20+%20BigNumber.add%280.05%2C%200.01%29%29%3B%0A%0ASystem.out.println%28%221.0%20-%200.42%20%3D%20%22%20+%20BigNumber.add%281.0%2C%200.42%29%29%3B%0A%0ASystem.out.println%28%224.015%20*%20100%20%3D%22%20+%20BigNumber.add%284.015%2C%20100%29%29%3B%0A%0ASystem.out.println%28%22123.3%20/%20100%20%3D%20%22%20+%20BigNumber.add%28123.3%2C%20100%29%29%3B%0A%0A%7D%0A%0A%7D%0A%60%60%60%0A%20%0A%0A%u8FD0%u884C%u7ED3%u679C%uFF1A%0A%0A0.060000000000000005%0A0.5800000000000001%0A401.49999999999994%0A1.2329999999999999%0A%u8BA1%u7B97n%u7684%u9636%u4E5830%21%20%3D%20265252859812191058636308480000000%0A%28bd1%20%3D%20new%20BigDecimal%280.1%29%29%20%3D%200.1000000000000000055511151231257827021181583404541015625%0A%28bd2%20%3D%20new%20BigDecimal%28%220.1%22%29%29%20%3D%200.1%0Abd2.equals%28bd3%29%20%3D%20false%0Abd2.compareTo%28bd3%29%20%3D%200%0A0.05%20+%200.01%20%3D%200.06%0A1.0%20-%200.42%20%3D%201.42%0A4.015%20*%20100%20%3D104.015%0A123.3%20/%20100%20%3D%20223.3%0A%0A%0A%23%23%u6E90%u7801%u5206%u6790%uFF1A%0ABigInteger%u4E0EBigDecimal%u90FD%u662F%u4E0D%u53EF%u53D8%u7684%uFF08immutable%uFF09%u7684%uFF0C%u5728%u8FDB%u884C%u6BCF%u4E00%u6B65%u8FD0%u7B97%u65F6%uFF0C%u90FD%u4F1A%u4EA7%u751F%u4E00%u4E2A%u65B0%u7684%u5BF9%u8C61%uFF0C%u7531%u4E8E%u521B%u5EFA%u5BF9%u8C61%u4F1A%u5F15%u8D77%u5F00%u9500%uFF0C%u56E0%u6B64%u5B83%u4EEC%u4E0D%u9002%u5408%u4E8E%u5927%u91CF%u7684%u6570%u5B66%u8FD0%u7B97%uFF0C%u5E94%u5C3D%u91CF%u4F7F%u7528long%u3001float%u3001double%u7B49%u57FA%u672C%u7C7B%u578B%u505A%u79D1%u5B66%u8BA1%u7B97%u6216%u8005%u5DE5%u7A0B%u8BA1%u7B97%u3002%u8BBE%u8BA1BigInteger%u4E0EBigDecimal%u7684%u76EE%u7684%u662F%u7528%u6765%u7CBE%u786E%u5730%u8868%u793A%u5927%u6574%u6570%u548C%u5C0F%u6570%uFF0C%u5E38%u7528%u4E8E%u5546%u4E1A%u8BA1%u7B97%u4E2D%u3002%0A%0ABigDecimal%u591F%u9020%u65B9%u6CD5%u7684%u53C2%u6570%u7C7B%u578B%u67094%u79CD%uFF0C%u5176%u4E2D%u7684%u4E24%u4E2A%u7528BigInteger%u6784%u9020%uFF0C%u53E6%u4E00%u4E2A%u662F%u7528double%u6784%u9020%uFF0C%u8FD8%u6709%u4E00%u4E2A%u4F7F%u7528String%u6784%u9020%u3002%60%u5E94%u8BE5%u907F%u514D%u4F7F%u7528double%u6784%u9020BigDecimal%60%uFF0C%u56E0%u4E3A%uFF1A%u6709%u4E9B%u6570%u5B57%u7528double%u6839%u672C%u65E0%u6CD5%u7CBE%u786E%u8868%u793A%uFF0C%u4F20%u7ED9BigDecimal%u6784%u9020%u65B9%u6CD5%u65F6%u5C31%u5DF2%u7ECF%u4E0D%u7CBE%u786E%u4E86%u3002%u6BD4%u5982%uFF0C%60new%20BigDecimal%280.1%29%u5F97%u5230%u7684%u503C%u662F0.1000000000000000055511151231257827021181583404541015625%u3002%60%u4F7F%u7528%60new%20BigDecimal%28%220.1%22%29%u5F97%u5230%u7684%u503C%u662F0.1%60%u3002%u56E0%u6B64%uFF0C%60%u5982%u679C%u9700%u8981%u7CBE%u786E%u8BA1%u7B97%uFF0C%u7528String%u6784%u9020BigDecimal%uFF0C%u907F%u514D%u7528double%u6784%u9020%uFF0C%u5C3D%u7BA1%u5B83%u770B%u8D77%u6765%u66F4%u7B80%u5355%uFF01%20%60%0A%0A----%0A-%20equals%28%29%u65B9%u6CD5%u8BA4%u4E3A0.1%u548C0.1%u662F%u76F8%u7B49%u7684%uFF0C%u8FD4%u56DEtrue%uFF0C%u800C%u8BA4%u4E3A0.10%u548C0.1%u662F%u4E0D%u7B49%u7684%uFF0C%u7ED3%u679C%u8FD4%u56DEfalse%u3002%u65B9%u6CD5compareTo%28%29%u5219%u8BA4%u4E3A0.1%u4E0E0.1%u76F8%u7B49%uFF0C0.10%u4E0E0.1%u4E5F%u76F8%u7B49%u3002%60%u6240%u4EE5%u5728%u4ECE%u6570%u503C%u4E0A%u6BD4%u8F83%u4E24%u4E2ABigDecimal%u503C%u65F6%uFF0C%u5E94%u8BE5%u4F7F%u7528compareTo%28%29%u800C%u4E0D%u662F%20equals%28%29%60%u3002%0A%0A-%20%u53E6%u5916%u8FD8%u6709%u4E00%u4E9B%u60C5%u5F62%uFF0C%u4EFB%u610F%u7CBE%u5EA6%u7684%u5C0F%u6570%u8FD0%u7B97%u4ECD%u4E0D%u80FD%u8868%u793A%u7CBE%u786E%u7ED3%u679C%u3002%u4F8B%u5982%uFF0C1%u9664%u4EE59%u4F1A%u4EA7%u751F%u65E0%u9650%u5FAA%u73AF%u7684%u5C0F%u6570%20.111111...%u3002%u51FA%u4E8E%u8FD9%u4E2A%u539F%u56E0%uFF0C%60%u5728%u8FDB%u884C%u9664%u6CD5%u8FD0%u7B97%u65F6%uFF0CBigDecimal%u53EF%u4EE5%u8BA9%u60A8%u663E%u5F0F%u5730%u63A7%u5236%u820D%u5165%60%u3002%0A%0A%23%23%23%u5E38%u7528%u65B9%u6CD5%0A%201.valueOf%28parament%29%3B%20%u5C06%u53C2%u6570%u8F6C%u6362%u4E3A%u5236%u5B9A%u7684%u7C7B%u578B%0A%u6BD4%u5982%20%0A%60%60%60%0Aint%20a%3D3%3B%0ABigInteger%20b%3DBigInteger.valueOf%28a%29%3B%0A%u5219b%3D3%3B%0AString%20s%3D%u201D12345%u201D%3B%0ABigInteger%20c%3DBigInteger.valueOf%28s%29%3B%0A%u5219c%3D12345%uFF1B%0A%60%60%60%0A%20%0A%0A%0A2.add%28%29%3B%20%u5927%u6574%u6570%u76F8%u52A0%0A%20%20%20%0A%20%20%20BigInteger%20a%3Dnew%20BigInteger%28%u201C23%u201D%29%3B%0A%0A%20%20%20BigInteger%20b%3Dnew%20BigInteger%28%u201C34%u201D%29%3B%0A%0A%20%20%20a.add%28b%29%3B%0A%0A---%0A%203.subtract%28%29%3B%20%u76F8%u51CF%0A%0A%204.multiply%28%29%3B%20%u76F8%u4E58%0A%0A%205.divide%28%29%3B%20%20%20%20%u76F8%u9664%u53D6%u6574%0A%0A%206.remainder%28%29%3B%u53D6%u4F59%0A%0A%207.pow%28%29%3B%20%20%20a.pow%28b%29%3Da%5Eb%0A%0A%208.gcd%28%29%3B%20%20%20%u6700%u5927%u516C%u7EA6%u6570%0A%0A%209.abs%28%29%3B%20%u7EDD%u5BF9%u503C%0A%0A%2010.negate%28%29%3B%u53D6%u53CD%u6570%0A%0A%2011.mod%28%29%3B%20a.mod%28b%29%3Da%25b%3Da.remainder%28b%29%3B%0A%0A%2012.max%28%29%3B%20min%28%29%3B%0A%0A%2013.punlic%20int%20comareTo%28%29%3B%0A%0A%2014.boolean%20equals%28%29%3B%20%u662F%u5426%u76F8%u7B49%0A%0A----%0A%23%23%23BigInteger%u6784%u9020%u51FD%u6570%uFF1A%0A%0A%20%20%u4E00%u822C%u7528%u5230%u4EE5%u4E0B%u4E24%u79CD%uFF1A%0A%0A%20%20%20BigInteger%28String%20val%29%3B%0A%0A%20%20%20%u5C06%u6307%u5B9A%u5B57%u7B26%u4E32%u8F6C%u6362%u4E3A%u5341%u8FDB%u5236%u8868%u793A%u5F62%u5F0F%uFF1B%0A%0A%20%20%20BigInteger%28String%20val%2Cint%20radix%29%3B%0A%0A%20%20%20%u5C06%u6307%u5B9A%u57FA%u6570%u7684BigInteger%u7684%u5B57%u7B26%u4E32%u8868%u793A%u5F62%u5F0F%u8F6C%u6362%u4E3ABigInteger%0A%0A----%0A%23%23%23%u57FA%u672C%u5E38%u91CF%uFF1A%0A%0A%20%20%20A%3DBigInteger.ONE%20%20%20%201%0A%0A%20%20%20B%3DBigInteger.TEN%20%20%20%2010%0A%0A%20%20%20C%3DBigInteger.ZERO%20%20%200