有一段經過Hibernate從PostgreSQL上進行sum統計的簡單代碼,但統計結果卻致使數據失真,不知緣由何在,求指教!數據庫
Java代碼片斷以下:app
public List<Object> getSalesRanking( ) throws Exception{ StringBuilder sbHql = new StringBuilder(); sbHql.append("select dl.dishId, dl.dishName, sum(dl.counts) as t, sum(dl.price) as s from R311dishList dl ") .append(" where dl.dishName='松仁玉米'") .append(" group by dl.dishId, dl.dishName"); String hql = sbHql.toString(); SQLQuery query = sf.getCurrentSession().createSQLQuery(hql); query.addScalar("dishId", StandardBasicTypes.STRING); query.addScalar("dishName", StandardBasicTypes.STRING); query.addScalar("t", StandardBasicTypes.DOUBLE); query.addScalar("s", StandardBasicTypes.DOUBLE); List<Object> list = new ArrayList<Object>(); ScrollableResults rs = query.scroll(); while(rs.next()){ try{ Object[] obj = new Object[4]; obj[0] = rs.getString(0); obj[1] = rs.getString(1); obj[2] = rs.getDouble(2); obj[3] = rs.getDouble(3); list.add(obj); } catch(Exception ex){ String err = ex.toString(); throw ex; } } return list; }
對應的數據庫表R311DishList中相關數據以下:ui
請注意,要統計的counts字段的數據分別是2.4/2.3/2.6,counts字段是double類型的,按道理sum(counts)後的結果應該是7.3,且經過SQL直接在數據庫上執行的結果也確實是7.3,以下圖:spa
但是,經過代碼執行獲得的結果倒是7.299999999999999:3d
修改數據庫中三條記錄的counts值爲其餘值均正常,且偶爾調整三條記錄的順序後也能正常顯示。不知何故??code