问:为什么使用BigDecimal计算浮点型数据?
浮点数没有办法是用二进制进行精确表示。我们的CPU表示浮点数由两个部分组成:指数和尾数,这样的表示方法一般都会失去一定的精确度,有些浮点数运算也会产生一定的误差。
因此在大多数的商业计算中,一般采用java.math.BigDecimal类来进行精确计算,最常见的是银行系统或者计费系统的结算。
针对double类型的数字有以下三种构造方法:
针对BigDecimal的构造方法的测试:
/**
* 使用几种构造方法之间比较
*/
@Test
public void constructorCompareTest() {
double d1 = 2.35;
double d2 = 2.0;
System.out.println("*********************BigDecimal(double val)(不允许使用)*********************");
System.out.println("使用BigDecimal BigDecimal(" + d1 + ")构造方法结果为:" + new BigDecimal(d1));
System.out.println("使用BigDecimal BigDecimal(" + d2 + ")构造方法结果为:" + new BigDecimal(d2));
System.out.println("*********************BigDecimal(String val)(推荐使用)***********************");
System.out.println("使用BigDecimal BigDecimal(\"" + String.valueOf(d1) + "\")构造方法结果为:" + new BigDecimal(String.valueOf(d1)));
System.out.println("使用BigDecimal BigDecimal(\"" + String.valueOf(d2) + "\")构造方法结果为:" + new BigDecimal(String.valueOf(d2)));
System.out.println("**************static BigDecimal valueOf(double val)(推荐使用)***************");
System.out.println("使用static BigDecimal valueOf(" + d1 + ")构造方法结果为:" + BigDecimal.valueOf(d1));
System.out.println("使用static BigDecimal valueOf(" + d2 + ")构造方法结果为:" + BigDecimal.valueOf(d2));
BigDecimal b1 = BigDecimal.valueOf(1);
BigDecimal b2 = BigDecimal.valueOf(1.00000);
System.out.println("b1使用equals比较b2:" + b1.equals(b2));
//compareTo结果:1代表b1>b2, 0代表b1=b2, -1代表b1
System.out.println("b1使用compareTo比较b2:" + b1.compareTo(b2));
}
打印结果如下:
*********************BigDecimal(double val)(不允许使用)*********************
使用BigDecimal BigDecimal(2.35)构造方法结果为:2.350000000000000088817841970012523233890533447265625
使用BigDecimal BigDecimal(2.0)构造方法结果为:2
*********************BigDecimal(String val)(推荐使用)***********************
使用BigDecimal BigDecimal("2.35")构造方法结果为:2.35
使用BigDecimal BigDecimal("2.0")构造方法结果为:2.0
**************static BigDecimal valueOf(double val)(推荐使用)***************
使用static BigDecimal valueOf(2.35)构造方法结果为:2.35
使用static BigDecimal valueOf(2.0)构造方法结果为:2.0
b1使用equals比较b2:false
b1使用compareTo比较b2:0
BigDecimal有如下7种舍入模式
/**
* Rounding mode to round away from zero.
* 向远离0的方向舍入
*/
public final static int ROUND_UP = 0;
/**
* Rounding mode to round towards zero.
* 向靠近0方向舍入
*/
public final static int ROUND_DOWN = 1;
/**
* Rounding mode to round towards positive infinity.
* 向正无穷方向舍入
*/
public final static int ROUND_CEILING = 2;
/**
* Rounding mode to round towards negative infinity.
* 向负无穷方向舍入
*/
public final static int ROUND_FLOOR = 3;
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round up.
* 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向上舍入, 1.55保留一位小数结果为1.6
*/
public final static int ROUND_HALF_UP = 4;
/**
* Rounding mode to round towards {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case round
* down.
* 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,向下舍入, 例如1.55 保留一位小数结果为1.5
*/
public final static int ROUND_HALF_DOWN = 5;
/**
* Rounding mode to round towards the {@literal "nearest neighbor"}
* unless both neighbors are equidistant, in which case, round
* towards the even neighbor.
* 向(距离)最近的一边舍入,除非两边(的距离)是相等,如果是这样,如果保留位数是奇数,使用ROUND_HALF_UP ,如果是偶数,使用ROUND_HALF_DOWN
*/
public final static int ROUND_HALF_EVEN = 6;
/**
* Rounding mode to assert that the requested operation has an exact
* result, hence no rounding is necessary.
* 计算结果是精确的,不需要舍入模式
*/
public final static int ROUND_UNNECESSARY = 7;
测试代码
/**
* BigDecimal保留小数位
*/
@Test
public void setScaleTest() {
BigDecimal b1 = BigDecimal.valueOf(3.145);
System.out.println("BigDecimal.ROUND_UP = " + BigDecimal.ROUND_UP + ", 处理 " + b1 + " 打印结果:" + b1.setScale(2, BigDecimal.ROUND_UP));
System.out.println("BigDecimal.ROUND_DOWN = " + BigDecimal.ROUND_DOWN + ", 处理" + b1 + "打印结果:" + b1.setScale(2, BigDecimal.ROUND_DOWN));
System.out.println("BigDecimal.ROUND_CEILING = " + BigDecimal.ROUND_CEILING + ",打印结果:" + b1.setScale(2, BigDecimal.ROUND_CEILING));
System.out.println("BigDecimal.ROUND_FLOOR = " + BigDecimal.ROUND_FLOOR + ",打印结果:" + b1.setScale(2, BigDecimal.ROUND_FLOOR));
System.out.println("BigDecimal.ROUND_HALF_UP = " + BigDecimal.ROUND_HALF_UP + ",打印结果:" + b1.setScale(2, BigDecimal.ROUND_HALF_UP));
System.out.println("BigDecimal.ROUND_HALF_DOWN = " + BigDecimal.ROUND_HALF_DOWN + ",打印结果:" + b1.setScale(2, BigDecimal.ROUND_HALF_DOWN));
System.out.println("BigDecimal.ROUND_HALF_EVEN = " + BigDecimal.ROUND_HALF_EVEN + ",打印结果:" + b1.setScale(2, BigDecimal.ROUND_HALF_EVEN));
System.out.println("BigDecimal.ROUND_UNNECESSARY = " + BigDecimal.ROUND_UNNECESSARY + ",打印结果:" + b1.setScale(2, BigDecimal.ROUND_UNNECESSARY));
}
打印结果
BigDecimal.ROUND_UP = 0, 处理 3.145 打印结果:3.15
BigDecimal.ROUND_DOWN = 1, 处理3.145打印结果:3.14
BigDecimal.ROUND_CEILING = 2,打印结果:3.15
BigDecimal.ROUND_FLOOR = 3,打印结果:3.14
BigDecimal.ROUND_HALF_UP = 4,打印结果:3.15
BigDecimal.ROUND_HALF_DOWN = 5,打印结果:3.14
BigDecimal.ROUND_HALF_EVEN = 6,打印结果:3.14
java.lang.ArithmeticException: Rounding necessary
at java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4148)
at java.math.BigDecimal.needIncrement(BigDecimal.java:4204)
at java.math.BigDecimal.divideAndRound(BigDecimal.java:4112)
at java.math.BigDecimal.setScale(BigDecimal.java:2452)
at com.leo.demo.BigDecimalTest.setScaleTest(BigDecimalTest.java:154)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
异常说明:
java.lang.ArithmeticException: Rounding necessary
at java.math.BigDecimal.commonNeedIncrement(BigDecimal.java:4148)
at java.math.BigDecimal.needIncrement(BigDecimal.java:4204)
出错原因精度丢失问题,要指定舍入模式或者扩大保留小数的位数
上面的最后可以做如下修改:
System.out.println("BigDecimal.ROUND_UNNECESSARY = " + BigDecimal.ROUND_UNNECESSARY + ",打印结果:" + b1.setScale(3, BigDecimal.ROUND_UNNECESSARY));
打印结果
BigDecimal.ROUND_UP = 0, 处理 3.145 打印结果:3.15
BigDecimal.ROUND_DOWN = 1, 处理3.145打印结果:3.14
BigDecimal.ROUND_CEILING = 2,打印结果:3.15
BigDecimal.ROUND_FLOOR = 3,打印结果:3.14
BigDecimal.ROUND_HALF_UP = 4,打印结果:3.15
BigDecimal.ROUND_HALF_DOWN = 5,打印结果:3.14
BigDecimal.ROUND_HALF_EVEN = 6,打印结果:3.14
BigDecimal.ROUND_UNNECESSARY = 7,打印结果:3.145
package com.leo.demo.bigdecimaltest;
/**
* @ClassName: BigDecimalTest
* @Description: 关于带浮点数据的计算
* @Author: leo825
* @Date: 2020-04-29 09:12
* @Version: 1.0
*/
import java.math.BigDecimal;
public class BigDecimalUtil {
// 除法运算默认精度
private static final int DEF_DIV_SCALE = 10;
private BigDecimalUtil() {
}
/**
* 精确加法
*/
public static double add(double value1, double value2) {
BigDecimal b1 = BigDecimal.valueOf(value1);
BigDecimal b2 = BigDecimal.valueOf(value2);
return b1.add(b2).doubleValue();
}
/**
* 精确减法
*/
public static double sub(double value1, double value2) {
BigDecimal b1 = BigDecimal.valueOf(value1);
BigDecimal b2 = BigDecimal.valueOf(value2);
return b1.subtract(b2).doubleValue();
}
/**
* 精确乘法
*/
public static double mul(double value1, double value2) {
BigDecimal b1 = BigDecimal.valueOf(value1);
BigDecimal b2 = BigDecimal.valueOf(value2);
return b1.multiply(b2).doubleValue();
}
/**
* 精确除法 使用默认精度
*/
public static double div(double value1, double value2) throws IllegalAccessException {
return div(value1, value2, DEF_DIV_SCALE);
}
/**
* 精确除法
*
* @param scale 精度
*/
public static double div(double value1, double value2, int scale) throws IllegalAccessException {
if (scale < 0) {
throw new IllegalAccessException("精确度不能小于0");
}
BigDecimal b1 = BigDecimal.valueOf(value1);
BigDecimal b2 = BigDecimal.valueOf(value2);
return b1.divide(b2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
* 四舍五入
*
* @param scale 小数点后保留几位
*/
public static double round(double v, int scale) throws IllegalAccessException {
return div(v, 1, scale);
}
/**
* 比较大小
*/
public static boolean equalTo(BigDecimal b1, BigDecimal b2) {
if (b1 == null || b2 == null) {
return false;
}
return 0 == b1.compareTo(b2);
}
public static void main(String[] args) throws IllegalAccessException {
double value1 = 1.1234223432344;
double value2 = 3.1415926535897;
BigDecimal value3 = BigDecimal.valueOf(value1);
BigDecimal value4 = BigDecimal.valueOf(value2);
System.out.println("精确加法=================" + BigDecimalUtil.add(value1, value2));
System.out.println("精确减法=================" + BigDecimalUtil.sub(value1, value2));
System.out.println("精确乘法=================" + BigDecimalUtil.mul(value1, value2));
System.out.println("精确除法 默认精度=================" + BigDecimalUtil.div(value1, value2));
System.out.println("精确除法 设置精度=================" + BigDecimalUtil.div(value1, value2, 20));
System.out.println("四舍五入 小数点后保留几位 =================" + BigDecimalUtil.round(value1, 10));
System.out.println("比较大小 =================" + BigDecimalUtil.equalTo(value3, value4));
}
}