![Error in float to long conversion [duplicate]](https://www.xszz.org/skin/wt/rpic/t24.jpg)
Question:
This question already has an answer here:
<ul><li> <a href="/questions/8911440/c-sharp-float-expression-strange-behavior-when-casting-the-result-float-to-int" dir="ltr" rel="nofollow">C# Float expression: strange behavior when casting the result float to int</a> <span class="question-originals-answer-count"> 7 answers </span> </li> </ul>I'm guessing this is due to floating point and accuracy but I just wanted to check to ensure that there's nothing else that I'm missing.
I've got a float (stored in info.Amount) of 1007.62 this gets multiplied by 100 and cast to a long. At this point I get a value of 100761.
<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/8EhGA.png" data-original="https://i.stack.imgur.com/8EhGA.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />
Is this just the good old rounding error that we encounter with Doubles being applied? The only way I can think this could happen is if underneath it's actually stored as 1007.6199999999999 (but rounded for display purposes). Then after the multiplication the conversion to a long ignores everything after the decimal point?
Answer1:You can use <a href="http://msdn.microsoft.com/de-de/library/system.convert.toint64%28v=vs.110%29.aspx" rel="nofollow">Convert.ToInt64</a> instead. This produces the correct value.
LinqPad-Testcode:
float z = 1007.62f;
z.Dump();
float x = z *100;
x.Dump();
long l = (long) (z*100);
l.Dump();
l = Convert.ToInt64(z*100);
l.Dump();
l = Convert.ToInt64(x);
l.Dump();
Result is:
1007,62
100762
100761
100762
100762
The cast (long) (z*100)
uses the CIL-instruction conv.i8
that converts to Int64, pushes Int64 on stack, while Convert.ToInt64
uses Math.Round(double)
to convert/cast to Int64.
A special note on conv.i8
is
Conversion from floating-point numbers to integer values truncates the number toward zero.
</blockquote> Answer2:Actually I think this is due to the variable you might declared.
Take the following sample:
double amount = 1007.62;
long result = (long) (amount*100);
result absolutely will be 100762 without any fraction or roundness. As a matter of fact, 1007.62 is a double number which should not be stored as float that may cause further problems like your situation.