十进制值的问题

时间:2010-08-31 11:24:49

标签: java hibernate jdbc

private static DecimalFormat decimalFormat = new DecimalFormat("0.0000000000");
    public static void main(String[] args) {
            String str ="73.71" ; 
            BigDecimal decimal= new BigDecimal(str);
            System.out.println("Tesing1 " + decimal.floatValue()/10000);
            System.out.println("Tesing2 " + decimal.floatValue());
            BigDecimal bigDecimal = new BigDecimal(decimalFormat.format(decimal.doubleValue()/ 10000));
            System.out.println("Tesing3 " + bigDecimal);        
        }

在上面的代码输出中

测试1 0.007371 测试2 73.71 测试3 0.0073710000

但是当我尝试使用hibernate将其保存到数据库中时,它的值变为74.执行舍入存根。

是否有人知道原因是什么。

使用此代码我保存对象

public boolean save(Object transInstance) {
        boolean lSuccess = false;
        getHibernateTemplate().save(transInstance);
        lSuccess = true;

        return lSuccess;
    }

这是表

中该列和列定义的.hbm文件条目
<property name="actlRsltPt" type="big_decimal">
            <column name="ACTL_RSLT_PT" precision="6" scale="4" />
        </property>

ACTL_RSLT_PT NUMBER (6,4)

1 个答案:

答案 0 :(得分:1)

如果Hibernate存在问题,请检查映射和数据库本身的比例,精度和数据类型的值。

另外,为什么要使用BigDecimal(任意精度带符号的十进制数)然后将其float值进行除法?这违背了使用BigDecimal并恢复浮点数学的目的,这可能会导致意外结果。您应该使用BigDecimal.divide(BigDecimal)方法(或其中一个重载)划分BigDecimal实例。