我是靠谱客的博主 魁梧蜡烛,这篇文章主要介绍BigInteger and BigDecimal,现在分享给大家,希望可以做个参考。

BigInteger::(java实现)

用于储存高精度非负整数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import java.math.BigInteger; public class Print1ToMaxOfNDigits { public static void main(String[] args) { // print1ToMaxDigits("9"); BigInteger bigInteger1 = new BigInteger("999999999999999999999999"); BigInteger bigInteger2 = new BigInteger("999999999999999999999998"); // add bigInteger1 = bigInteger1.add(bigInteger2); System.out.println(bigInteger1); // subtract bigInteger1 = bigInteger1.subtract(bigInteger2); System.out.println(bigInteger1); // multiplay bigInteger1 = bigInteger1.multiply(bigInteger2); System.out.println(bigInteger1); // divide bigInteger1 = bigInteger1.divide(bigInteger2); System.out.println(bigInteger1); // negate 取相反数 bigInteger1 = bigInteger1.negate(); System.out.println(bigInteger1); } }

BigDecimal:

高精度浮点数运算



复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import java.math.BigDecimal; /* * 构造方法: * public BigDecimal(String val): * 成员方法: * public BigDecimal add(BigDecimal augend):加 * public BigDecimal subtract(BigDecimal subtrahend):减 * public BigDecimal multiply(BigDecimal multiplicand):乘 * public BigDecimal divide(BigDecimal divisor):除 * public BigDecimal divide(BigDecimal divisor,int scale,int roundingMode):商,几位小数,如何舍取。 * */ public class BigDecimalDemo { public static void main(String[] args) { System.out.println(0.09 + 0.01); System.out.println(1.0 - 0.32); System.out.println(1.015 * 100); System.out.println(1.301 / 100); // public BigDecimal add(BigDecimal augend):加 BigDecimal bd1 = new BigDecimal("0.09"); BigDecimal bd2 = new BigDecimal("0.01"); System.out.println("add:" + bd1.add(bd2)); System.out.println("----------------------"); // public BigDecimal subtract(BigDecimal subtrahend):减 BigDecimal bd3 = new BigDecimal("1.0"); BigDecimal bd4 = new BigDecimal("0.32"); System.out.println("subtract:" + bd3.subtract(bd4)); System.out.println("----------------------"); // public BigDecimal multiply(BigDecimal multiplicand):乘 BigDecimal bd5 = new BigDecimal("1.015"); BigDecimal bd6 = new BigDecimal("100"); System.out.println("multiply:" + bd5.multiply(bd6)); System.out.println("----------------------"); // public BigDecimal divide(BigDecimal divisor):除 BigDecimal bd7 = new BigDecimal("1.301"); BigDecimal bd8 = new BigDecimal("100"); System.out.println("divide:" + bd7.divide(bd8)); System.out.println("divide:" + bd7.divide(bd8,3,BigDecimal.ROUND_HALF_UP)); System.out.println("divide:" + bd7.divide(bd8,9,BigDecimal.ROUND_HALF_UP)); } }


最后

以上就是魁梧蜡烛最近收集整理的关于BigInteger and BigDecimal的全部内容,更多相关BigInteger内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(61)

评论列表共有 0 条评论

立即
投稿
返回
顶部