1

I have a application using nhibernate and fluent-nhibernate. We are using SQL Server 2008 and I have a table with a float column because we do not have a fixed precision and the user can set any number of decimal places (1, 10, 25 decimal places, etc..). I map it using fluent-nhibernate like this:

Map(x => x.Factor).Column("FACTOR").Not.Nullable();

And I also tried:

Map(x => x.Factor).Column("FACTOR").Precision(53).Not.Nullable();

and this:

Map(x => x.Factor).Column("FACTOR").CustomSqlType("float").Not.Nullable();

and it does not work. Look my database screenshot:

enter image description here

The field factor is my float column, the last registry have the number 1,234567891 and when the nhibernate hits a query on database, it comes in C# with 1,123456788, the C# is rounding the value. We need the original value.

How can I get it working?

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194

1 Answers1

1

Yes, use decimal in the database, the float will have rounding problems that you have been experiencing. Also use decimal in the domain class because both float/double suffer from the same rounding issues.

To map use:

Map(x => x.Factor).Column("FACTOR").CustomSqlType("decimal(p,s)").Not.Nullable();

This should translate to the decimal in sql server.

h.alex
  • 902
  • 1
  • 8
  • 31
  • Alex, thank you for the awnser, but, how can I configure the `Precision` and `Scale` to get the max of decimal? I mean, max of integer and decimals places. – Felipe Oriani Jul 24 '13 at 11:04
  • By changing the (p,s). The p is the total length of the decimal (in digits), the s is the number of decimal places. But this mapping will be permanent - as it should - it is the database schema! It should not change often in a finished application. If you want to have arbitrary precision in every row, which is not what SQL standard intended to offer in my opinion, you could encode the value into a bitstream and store a byte[]. But then, good luck with queries :) – h.alex Jul 24 '13 at 11:40
  • Thank you Alex, do you know, what column type i can create in Oracle database to get it working. In sql server I solve with `decimal (29,14)` and in oracle? I tried with `number(29,14)` but does not work! – Felipe Oriani Jul 24 '13 at 13:37
  • My Oracle experience is limited, but seems they use [decimal](http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj15260.html). See also [here](http://stackoverflow.com/questions/11031409/decimals-p-or-numbers-p). – h.alex Jul 24 '13 at 13:45