insane

insane

  • NA
  • 3
  • 0

How do I fix this convert error? Is this a bug?

Dec 10 2012 9:45 PM
I have an integer and it's value is -16777216
I want to convert it to a float once done the float equals -1.677722E+07
Now if I convert the float back at this point everything works and I get a proper result.

However if I manually make the float equal -1.677722E+07 then convert back to int I get
a wrong result of -16777220 what is going on and how do I fix this? Example shown below in c# code.
 
int i = -16777216;
float f = Convert.ToSingle(i);                        // = -1.677722E+07 (works converting back)
label1.Text = i.ToString();
label2.Text = f.ToString();
f = -1.677722E+07f;                                   //Putting it in as a value makes error result
label3.Text = Convert.ToInt32(f).ToString();   //converting back

Answers (4)