pango wrote:
I write below code in my program:
float f=0.371f;
int i=(int)(f*1000.0f);
I think the result of "i" should be "371",but in fact it is "370",why?How to solve it?
If you look at f in your debugger you will see it is actually
0.37099999. This is the nature of floating point representation. The
other key thing to note is that float to integer conversions don't
round. If you want rounding, you write your own.
And, as an exercise in confusion, inspect this with your debugger:
int i2 = (0.371 * 1000.0);
doug.