最近有空寫了點老的j2ee的代碼,發現有一個十分有意思的問題,當用hibernate從資料庫裡把浮點數讀取出來的時候做一些比如累加的工作,例如 summary 或者遞減之類的,就會發現在最後的結果中會出現些許問題。
如:3.41+5.2+56.2+23.3+... (這類兩位小數的價錢),結果會出現103.00000000000001這種結果,但是人算的話反而會得出正常的資料。看樣子double,float這類資料精度上來了還會有這類問題。
于是,翻了點資料,在有的程式設計語言中提供了專門的貨币類型來處理這種情況,但是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:
package com.common.util;
import java.math.bigdecimal;
public final class arith {
// 預設除法運算精度
private static final int def_div_scale = 2;
// 這個類不能執行個體化
private arith() {
}
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();
public static double sub(double v1, double v2) {
return b1.subtract(b2).doublevalue();
public static double mul(double v1, double v2) {
return b1.multiply(b2).doublevalue();
public static double div(double v1, double v2) {
return div(v1, v2, def_div_scale);
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");
}
return b1.divide(b2, scale, bigdecimal.round_half_up).doublevalue();
public static double round(double v, int scale) {
bigdecimal b = new bigdecimal(double.tostring(v));
bigdecimal one = new bigdecimal("1");
return b.divide(one, scale, bigdecimal.round_half_up).doublevalue();
呵呵,float和double隻能用來做科學計算或者是工程計算,在商業計算中我們要用 java.math.bigdecimal。這個才是解決的方式,我也試過了發現确實不錯。不過話又說回來了,在.net裡似乎沒有發現有這類問題呀,能說他好麼?我看未必,有時候封裝的太好,功能太多,往往讓人忘記了這些背後本來的面目:)