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
Loading
VulpesPosted Dec 11, 2012, 6:54 AM
insanePosted Dec 11, 2012, 2:02 AM
Veena SardaPosted Dec 11, 2012, 1:51 AM
The +07f is the problem area. You are not representing the scientific notation correctly.Try as shown in the screenshot
Thanks
Veena
Blocked AccountPosted Dec 10, 2012, 11:38 PM
Integer To Float Conversion:
int intNo = 2345;
float no = intNo;
Console.Write(no); // float can handle int because of upcasting
Float To Integer Conversion:
float floNo = 1.5f;
int num = (int)floNo; // but here .5 value will be lost due to downcasting
Console.Write(num);
so when you converting back float to int, it can be some value loss. this is the concept.
i have made some changes to your 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 = ((int)(f)).ToString(); //converting back
try this