java.math 包中的两个很有用的类:Biglnteger 和 BigDecimal
这两个类可以处理包含任意长度数字序列的数值。
Biglnteger 类实现了任意精度的整数运算, BigDecimal 实现了任意精度的浮点数运算。
BigInteger
使用静态的 valueOf方法可以将普通的数值转换为大数值,但是不能使用熟悉的算术运算符(如:+ 和 *) 处理大数值。 而需要使用大数值类中的 add 和 multiply 方法。
BigInteger a = BigInteger.valueof(100);
BigInteger b = BigInteger.valueof(20);
BigInteger c = a.add(b); // c = a + b
Biglnteger d = c.multiply(b.add(BigInteger.valueOf(2))); // d = c * (b + 2)
// b += a; // false
// b += 2; // false
| BigInteger方法: | |
|---|---|
| Biglnteger add(Biglnteger other) | 和 |
| Biglnteger subtract(Biglnteger other) | 差 |
| Biglnteger multipiy(BigInteger other) | 积 |
| Biglnteger divide(Biglnteger other) | 商 |
| Biglnteger mod(Biglnteger other) | 余数 |
| int compareTo(Biglnteger other) | 相等, 返回 0; 比other小 返回-1; 比other大 返回1。 |
| static Biglnteger valueOf(long x) | 返回值等于 x 的大整数。 |
int x = a.compareTo(b);
System.out.println(x); // 1
int y = b.compareTo(a);
System.out.println(y); // -1
BigInteger z = BigInteger.valueOf(5);
System.out.println(z); // 5
BigDecimal
| BigDecimal方法: | |
|---|---|
| • BigDecimal add(BigDecimal other) | 和 |
| • BigDecimal subtract(BigDecimal other) | 差 |
| • BigDecimal multipiy(BigDecimal other) | 积 |
| • BigDecimal divide(BigDecimal other, RoundingMode mode) | 计算商, 必须给出舍入方式 ( 输入RoundingMode)。 如:RoundingMode.HALFUP 是四舍五入。它适用于常规的计算。 |
| • int compareTo(BigDecimal other) | 相等,返回 0 ; 小于other,返回-1;大于other,返回1。 |
| • static BigDecimal valueOf(long x) | 返回值为 x 的一个大实数。 |
| • static BigDecimal valueOf(long x ,int scale) | 返回值为 x / (10^scale) 的一个大实数。 |
| RoundingMode | |
|---|---|
| RoundingMode.UP | 上取整 |
| RoundingMode.DOWN | 去掉小数部分取整,也就是正数取左边,负数取右边,相当于向原点靠近的方向取整 |
| RoundingMode.CEILING | 取右边最近的整数 |
| RoundingMode.FLOOR | 取左边最近的正数 |
| RoundingMode.HALF_UP | 四舍五入,负数原理同上 |
| RoundingMode.HALF_DOWN | 五舍六入,负数先取绝对值再五舍六入再负数 |
| RoundingMode.HALF_EVEN | 这个比较绕,整数位若是奇数则四舍五入,若是偶数则五舍六入 |
不同取整模式下的取整操作总结
| 操作数 | UP | DOWN | CEILING | FLOOR | HALF_UP | HALF_DOWN | HALF_EVEN | UNNECESSARY |
|---|---|---|---|---|---|---|---|---|
| 5.5 | 6 | 5 | 6 | 5 | 6 | 5 | 6 | throw ArithmeticException |
| 2.5 | 3 | 2 | 3 | 2 | 3 | 2 | 2 | throw ArithmeticException |
| 1.6 | 2 | 1 | 2 | 1 | 2 | 2 | 2 | throw ArithmeticException |
| 1.1 | 2 | 1 | 2 | 1 | 1 | 1 | 1 | throw ArithmeticException |
| 1.0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| -1.0 | -1 | -1 | -1 | -1 | -1 | -1 | -1 | -1 |
| -1.1 | -2 | -1 | -1 | -2 | -1 | -1 | -1 | throw ArithmeticException |
| -1.6 | -2 | -1 | -1 | -2 | -2 | -2 | -2 | throw ArithmeticException |
| -2.5 | -3 | -2 | -2 | -3 | -3 | -2 | -2 | throw ArithmeticException |
| -5.5 | -6 | -5 | -5 | -6 | -6 | -5 | -6 | throw ArithmeticException |
BigDecimal a = new BigDecimal("5.677");
BigDecimal b = new BigDecimal("3.0");
BigDecimal c = a.divide(b,RoundingMode.HALF_UP); // 1.892
保留小数方式:setScale(int num,RoundingMode)
BigDecimal d = a.setScale(2,RoundingMode.HALF_UP);// 5.68
BigDecimal的scale函数返回小数位数。
int x = d.scale();// 返回小数的位数,这里返回3
最后
以上就是还单身小丸子最近收集整理的关于Java BigInteger和BigDecimal的全部内容,更多相关Java内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复