NHibernate 3.0 rounding a decimal to 5 decimal places - why?

Tag: nhibernate , decimal , precision Author: xgocn Date: 2011-02-21


We were using NHiberate 2.1 where we are storing decimal values (exchange rates) e.g. 123.1234567 to 7 decimal places

We are mapping the type using default mapping style:

<property name="ExchangeRate"  not-null="true" />

However when we upgrade to NHibernate 3.0 the value above is saved as 123.1234500.

It does not specify this change in behaviour in the 3.0 release notes although it does seem to detail that in issue [NH-1594], the default value for decimal is DECIMAL(19,5)

We have a solution i.e. specify the following mapping:

<property name="ExchangeRate"  type="decimal(10,7) not-null="true" />

I need to know is this solution the right way to solve this issue? Also, why is there a functional change in behaviour with rounding between 2.1 and 3.0?


Billy Stack

Are you sure it isn't because of the database design? Are you using the same database schema when trying 3.0?
Sorry, That should have been type="decimal(14,7)
Yes, exact same database design. If I flip back to the older version of NHibernate, the issue does not exist.

Best Answer

That works, but this is cleaner IMO:

<property name="ExchangeRate" precision="10" scale="7" />

A not-null decimal is implied by the property type.


I would prefer the other way to be honest. Thanks for the suggestion however.
My question however is why it it rounding to 5 decimal places by default in NH 3, whereas it didn't do this in NH2.1...
@bstack that's the kind of question you can ask in the nhibernate-development list. Anyway, exact numeric types should be deterministic, so it's better to be explicit than to trust an undocumented default.