Vicky
Vicky

Reputation: 9575

Problem in Decimal Value

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);        
        }

In the Above code out put is

Tesing1 0.007371 Tesing2 73.71 Tesing3 0.0073710000

But when i try to save it into database using hibernate its value become as 74. Doing rounding stub.

Does any one know what is reason.

Using this code i am saving the object

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

        return lSuccess;
    }

and this is .hbm file entry for that column and column defination in table

<property name="actlRsltPt" type="big_decimal">
            <column name="ACTL_RSLT_PT" precision="6" scale="4" />
        </property>

ACTL_RSLT_PT NUMBER (6,4)

Upvotes: 0

Views: 388

Answers (1)

matt b
matt b

Reputation: 140061

If there is an issue with Hibernate, check what values for scale and precision and datatype you have in your mappings and in the database itself.

Also, why are you taking a BigDecimal (an arbitrary-precision signed decimal number) and then taking its float value to do division? This defeats the purpose of using a BigDecimal and reverts back to floating point math which may lead to unexpected results. You should divide BigDecimal instances with the BigDecimal.divide(BigDecimal) method (or one of it's overloads).

Upvotes: 1

Related Questions