3

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

  • possible duplicate of [Rounding double values in C#](http://stackoverflow.com/questions/2129804/rounding-double-values-in-c-sharp) – Fendy Mar 11 '15 at 08:31
  • How did you even get that the generated statement contains `86.609001159668`? Isn't the value passed as a DbParameter? – Luaan Mar 11 '15 at 08:34

1 Answers1

4

.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.

Luaan
  • 62,244
  • 7
  • 97
  • 116