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.  [ 4 posts ] 
Author Message
 Post subject: losing precision on "real" data types from SQL Ser
PostPosted: Mon Jan 29, 2007 1:20 pm 
Beginner
Beginner

Joined: Mon Oct 02, 2006 6:46 pm
Posts: 32
Hibernate version: NHibernate1.2.0Beta2

I have a column in a database of type "real". The data in the database can have a high precision (1.40000001 for example). However, whenever I try to Get a row in that table the value is rounded and I lose precision. 1.40000001 becomes 1.4 which throws off our shipping calculations for some countries. I have tried a multitude of things:
    Straight nHibernate Gets
    ExecuteScalar with a stored proc
    ExecuteNonQuery into a dataset from the stored proc
    A raw SELECT statement

The type in the database is "real" the nhibernate auto-generated mapping file has the type set at "single". The .NET class is of course also "single". I have tried changing it to other types such as float, and decimal but the problem still occurs.

I have searched on the forums for ways to get around this rounding issue but didn't find anything.

There are also other properties in the same mapping file of type "decimal" that are getting their full precision on a Get. These columns are alos of a different type than "real" in the database.

example:

Code:
<property column="Weight" type="System.Decimal" name="Weight" />
<property column="RetailPrice" type="System.Decimal" name="RetailPrice" />

Weight comes back with rounded precision whereas RetailPrice comes back with its full precision.

Please help, thanks:)


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 29, 2007 4:54 pm 
Newbie

Joined: Fri Mar 24, 2006 4:46 pm
Posts: 13
From BOL:
float and real
Approximate number data types for use with floating point numeric data. Floating point data is approximate; not all values in the data type range can be precisely represented.


In my experience it is a bad idea to use the real data type when storing values that need precise precision, unexpected behavior can and often does occur.
You could try converting the value to a money type in the select statement.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 29, 2007 5:55 pm 
Beginner
Beginner

Joined: Mon Oct 02, 2006 6:46 pm
Posts: 32
I have tried converting it to a few different types.

I can change the type but it would involve a lot of effort and there are a lot of other applications that are able to get the data with it's full precision without issue. I even have a legacy app that works just like the nhibernate app I'm working on now that doesn't have any issue. It seems like in all my testing it is directly related to nHibernate. Maybe I'm mistaken.


Top
 Profile  
 
 Post subject: Real
PostPosted: Tue Jan 30, 2007 12:38 am 
Beginner
Beginner

Joined: Mon Jan 08, 2007 11:59 pm
Posts: 31
Real in SQL Server is equivalent to Float(24) and it can have a precision of atmost 7 digits.

I created a small table with a real column and tried to insert your value 1.40000001 from the Management Studio and it truncates it to 1.4

You will be better of changing it to more precise data type.

Job Samuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 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.