简体   繁体   中英

How to ensure a precison on float using entity framework 6.x and a float column

I tried to understand why it seems not to be possible to exactly set a precision for a float / double value.

However .NET allows me to use Math.Round on such values an the inspector Shows me exactly what I rounded and / or entered.

Whenever I try to save such a value back to a table I get a Statement containing 86,609001159668 instead of 86,609

There seems so be now way in which the entered value reaches the table correctly.

I could change anything in my code to achieve this Goal but not the columntype itself.

Any ideas?

Thanks,

Chris

.NET floats aren't decimal-precise. The only BCL decimal type that is decimal-precise is decimal . float and double are binary "decimals", so there's no way to represent 86.609 precisely in them. Using Math.Round simply brings the value close enough to 86.609 that it's printed out as such during the rounding happening as part of ToString .

So if you want precise decimal-point decimals, use decimal in .NET.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM