-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 
Author Message
 Post subject: Precision lost mapping decimal(16,4) to BigDecimal
PostPosted: Fri Jul 13, 2007 5:14 pm 
Newbie

Joined: Wed Mar 07, 2007 12:32 pm
Posts: 7
In an Informix database I have a decimal(16,4) column with a value of 99.9991. After hydrating an object pointing to this row, the BigDecimal has a value of 99.9990999999999985448084771633148193359375

I've set the column type="big_decimal" but the behavior remains the same.

What other things should I look for?

Thanks,
Jay


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 9:02 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
Hi Jay

Looks like your value has been converted to a double at some point.

Compare the following:

System.out.println(new BigDecimal(99.9991)); // converted to double value
99.9990999999999985448084771633148193359375
- converted to a double before becoming a BD
- same value as you're seeing

System.out.println(new BigDecimal("99.9991")); // quoted value converted by BD
99.9991
- straight BD conversion from string

Your database column has 16,4 precision/scale so the conversion must be occuring after you read the data back from the DB. In my test, trying to store new BigDecimal(99.9991) (i.e. initialised with double) into a a 16,4 fails with a data truncation exception.

Do you retieve the data from DB as a double then convert it to a BD?

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 9:19 pm 
Newbie

Joined: Wed Mar 07, 2007 12:32 pm
Posts: 7
First, thanks for the reply.

Next, "no", the BigDecimal variable is mapped directly to the database column and populated by Hibernate.

I agree in your assessment that it's being converted to a double at some point, but not sure where to track it down.

Thanks again.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jul 13, 2007 9:37 pm 
Expert
Expert

Joined: Fri Jul 13, 2007 8:18 am
Posts: 370
Location: london
If you can narrow this down and post a simple test case it will definitely help identify the problem - e.g. a very basic persisted pojo and mapping file (or annotations) with a single BigDecimal property that fails in the same way when reading back the stored value 99.9991.

Don't know much about informix but with both MYSQL and HSQLDB I stored a BigDecimal value of 99.9991 in a 16,4 column and it re-hydrated to the same value;

Mike


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jul 16, 2007 11:34 am 
Newbie

Joined: Wed Mar 07, 2007 12:32 pm
Posts: 7
Turns out the problem was related to a faulty test case initializing a BigDecimal with a double. These problems tend to be of the user error variety. Thanks for your help.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 5 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.