Try the code:
float a = 0.77F;
int b = (int)(100 * a);
Console.WriteLine(b.ToString());
Result is 76.
What do you think about such strange .Net calculate execution ?
Printable View
Try the code:
float a = 0.77F;
int b = (int)(100 * a);
Console.WriteLine(b.ToString());
Result is 76.
What do you think about such strange .Net calculate execution ?
Nothing new! FAQ
simple solution :)
Code:decimal a = 0.77M;
int b = (int)(100 * a);
Console.WriteLine(b.ToString());
can also do it with floatCode:float a = 0.77f;
int b = Convert.ToInt32(100 * a);
console.WriteLine(b.ToString());
Solution is not the problem.Quote:
Originally Posted by Syrix
I used Round, for example.
When I met the problem I was suppressed by so non-clear behavior of the code.
I always used double before.
Float was returned as object from DataRow element from correspond DB Field:-)
And I do not understand, why 100*0.77f == 76.99999…
Is it Microsoft or Intel?
it is because of the processor can never represent exactly 0.77 in binary.Quote:
Originally Posted by Alex Rest
That's why I posted the FAQ...Quote:
Originally Posted by Alex Rest