运用BigDecimal精确计算,定义精度,取舍原则
问题的提出:
如果我们编译运行下面这个程序会看到什么?
public class Test{
public static void main(String args[]){
System.out.println(0.05+0.01);
System.out.println(1.0-0.42);
System.out.println(4.015*100);
System.out.println(123.3/100);
}
};
你没有看错!结果确实是
0.060000000000000005
0.5800000000000001
401.49999999999994
1.2329999999999999
Java 中的简单浮点数类型 float 和 double 不能够进行运算。不光是 Java ,在其它很多编程语言中也有这样的问题。在大多数情况下,计算的结果是准确的,但是多试几次(可以做一个循环)就可以试出类似上面的错误。现在终于理解为什么要有 BCD
这个问题相当严重,如果你有 9.999999999999 元,你的计算机是不会认为你可以购买 10
在有的编程语言中提供了专门的货币类型来处理这种情况,但是 Java
四舍五入
我们的第一个反应是做四舍五入。 Math 类中的 round
public double round(double value){
return Math.round(value*100)/100.0;
}
非常不幸,上面的代码并不能正常工作,给这个方法传入 4.015 它将返回 4.01 而不是 4.02
4.015*100=401.49999999999994
因此如果我们要做到精确的四舍五入,不能利用简单类型做任何运算
java.text.DecimalFormat
System.out.println(new java.text.DecimalFormat("0.00").format(4.025));
输出是 4.02
BigDecimal
在《 Effective Java 》这本书中也提到这个原则, float 和 double 只能用来做科学计算或者是工程计算,在商业计算中我们要用 java.math.BigDecimal 。 BigDecimal 一共有 4 个够造方法,我们不关心用 BigInteger
BigDecimal(double val)
Translates a double into a BigDecimal.
BigDecimal(String val)
Translates the String repre sentation of a BigDecimal into a BigDecimal.
上面的 API
Note: the results of this constructor can be somewhat unpredictable. One might assume that new BigDecimal(.1) is exactly equal to .1, but it is actually equal to .1000000000000000055511151231257827021181583404541015625. This is so because .1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the long value that is being passed in to the constructor is not exactly equal to .1, appearances nonwithstanding.
The (String) constructor, on the other hand, is perfectly predictable: new BigDecimal(".1") is exactly equal to .1, as one would expect. Therefore, it is generally recommended that the (String) constructor be used in preference to this one.
原来我们如果需要精确计算,非要用 String 来够造 BigDecimal 不可!在《 Effective Java 》一书中的例子是用 String 来够造 BigDecimal
解决方案
现在我们已经可以解决这个问题了,原则是使用 BigDecimal 并且一定要用 String
但是想像一下吧,如果我们要做一个加法运算,需要先将两个浮点数转为 String ,然后够造成 BigDecimal ,在其中一个上调用 add 方法,传入另一个作为参数,然后把运算的结果( BigDecimal )再转换为浮点数。你能够忍受这么烦琐的过程吗?下面我们提供一个工具类 Arith
public static double add(double v1,double v2)
public static double sub(double v1,double v2)
public static double mul(double v1,double v2)
public static double div(double v1,double v2)
public static double div(double v1,double v2,int scale)
public static double round(double v,int scale)
附录
源文件 Arith.java
import java.math.BigDecimal;
/**
* 由于 Java
*
*/
public class Arith{
//
private static final int DEF_DIV_SCALE = 10;
//
private Arith(){
}
/**
*
* @param v1
* @param v2
* @return
*/
public static double add(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.add(b2).doubleValue();
}
/**
*
* @param v1
* @param v2
* @return
*/
public static double sub(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.subtract(b2).doubleValue();
}
/**
*
* @param v1
* @param v2
* @return
*/
public static double mul(double v1,double v2){
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.multiply(b2).doubleValue();
}
/**
*
* 小数点以后 10
* @param v1
* @param v2
* @return
*/
public static double div(double v1,double v2){
return div(v1,v2,DEF_DIV_SCALE);
}
/**
* 提供(相对)精确的除法运算。当发生除不尽的情况时,由 scale
*
* @param v1
* @param v2
* @param scale
* @return
*/
public static double div(double v1,double v2,int scale){
if(scale<0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b1 = new BigDecimal(Double.toString(v1));
BigDecimal b2 = new BigDecimal(Double.toString(v2));
return b1.divide(b2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
/**
*
* @param v
* @param scale
* @return
*/
public static double round(double v,int scale){
if(scale<0){
throw new IllegalArgumentException(
"The scale must be a positive integer or zero");
}
BigDecimal b = new BigDecimal(Double.toString(v));
BigDecimal one = new BigDecimal("1");
return b.divide(one,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
}
};
参数类型为 MathContext 的 java.math
参数类型为 MathContext 的 java.math | |
|
返回其值为此 BigDecimal 绝对值的 BigDecimal |
|
返回其值为 (this + augend) 的 BigDecimal |
|
返回其值为 (this / divisor) 的 BigDecimal |
|
返回由两个元素组成的 BigDecimal 数组,该数组包含 divideToIntegralValue 的结果,后跟根据上下文设置对两个操作数进行舍入计算所得到的 remainder |
|
返回 BigDecimal ,其值为 (this / divisor) |
|
返回其值为 (this × multiplicand) 的 BigDecimal |
|
返回其值为 (-this) 的 BigDecimal |
|
返回其值为 (+this) 的 BigDecimal |
|
返回其值为 (thisn ) 的 BigDecimal |
|
返回其值为 (this % divisor) 的 BigDecimal |
|
返回根据 MathContext 设置进行舍入后的 BigDecimal |
|
返回其值为 (this - subtrahend) 的 BigDecimal |
参数类型为 MathContext 的 java.math | |
将 BigInteger 非标度值和 int 标度转换为 BigDecimal | |
将 BigInteger 转换为 BigDecimal | |
将 BigDecimal 的字符数组表示形式转换为 BigDecimal ,接受与 | |
将 BigDecimal 的字符数组表示形式转换为 BigDecimal ,接受与 | |
将 double 转换为 BigDecimal | |
将 int 转换为 BigDecimal | |
将 long 转换为 BigDecimal | |
将 BigDecimal 的字符串表示形式转换为 BigDecimal ,接受与 |