开发者

Getting float value from the database

开发者 https://www.devze.com 2023-03-26 12:30 出处:网络
I\'m using SQL Server and in one of the tables I have a nullable float column.When I look at that value in Microsoft SQL Server Management Studio I have a value in one of the records as 1.3.I also hav

I'm using SQL Server and in one of the tables I have a nullable float column. When I look at that value in Microsoft SQL Server Management Studio I have a value in one of the records as 1.3. I also have a client app that gets data from the database via Entity Framework (EF) and it has a corresponding nullable double (double?) property for that column. Now when I get a value from the database it has 1.2999999523162842 values in it.

All these records (more than 1 million) will be used in interpolation, extrapolation, calculation and any smallest deviation becomes a significant value. So all values on the client side have to match values in the database.

I can't round on the client side to the first decimal number, because in the database there are values with 2, 3, 4, or more decimal points.

My questio开发者_运维问答n is how can I make sure that all values in the database will be the same values on the client after loading them from database via EF?


Isn't float a binary floating point value in SQL Server? If so, your value isn't really 1.3, as that isn't exactly representable in binary floating point. Options:

  • Perhaps your field is effectively a decimal floating (or fixed) point type, in which case you should use decimal on the .NET side as well.
  • If you're using binary floating point in SQL server, I'd expect the result in .NET to be the same: the nearest binary floating point value of the appropriate type to the number you originally entered. Just because SQL Server is showing you the value as 1.3 doesn't mean it is exactly 1.3.

Using my DoubleConverter, it looks like the closest double value to 1.3 is actually 1.3000000000000000444089209850062616169452667236328125 - so I'm not sure why you'd be getting the value you're actually seeing. The closest float value is 1.2999999523162841796875, which sounds like it's what you're getting. So it's as if your value in the database is being converted as a 32-bit floating point number instead of a 64-bit one.

Is your database field actually something like FLOAT(23)? If so, that corresponds with a .NET float instead of double. That would explain things.

0

精彩评论

暂无评论...
验证码 换一张
取 消