B
Bill Ataras via .NET 247
I need to be able to take a dollars and cents amount stored in asingle, multiply it by 100 and store it in an Int32 as thenumber of pennies. Ie, I want to convert a Single of $1.23F intoan Int32 of 1230. I'm getting unreliable behavior from Singlesthough. Here is code to reproduce the problem:
Problem 1:
Single val = 0.01F; // Start with a penny
Int32 ival = (Int32)(val*100.0F);
Single sval = val * 100.0F;
In this problem, ival becomes 0 and sval becomes 1.0, yet valshows up as 0.01
Problem 2:
Single val = 0.53F;
Single sval = val * 100.0F;
Int32 ival = (Int32)sval;
Int32 ival2 = (Int32)(val*100.0F);
In this problem, ival and ival2 become 52 and sval becomes52.9999962, yet val shows up as 0.53
This looks like a bug to me. And no, I can't switch to doubles.And I can't use Convert.ToInt32 as it rounds and my original'val' may have a sub-penny value that I want truncated (ie1.2357F should convert to 1230 Int32).
thanks
Problem 1:
Single val = 0.01F; // Start with a penny
Int32 ival = (Int32)(val*100.0F);
Single sval = val * 100.0F;
In this problem, ival becomes 0 and sval becomes 1.0, yet valshows up as 0.01
Problem 2:
Single val = 0.53F;
Single sval = val * 100.0F;
Int32 ival = (Int32)sval;
Int32 ival2 = (Int32)(val*100.0F);
In this problem, ival and ival2 become 52 and sval becomes52.9999962, yet val shows up as 0.53
This looks like a bug to me. And no, I can't switch to doubles.And I can't use Convert.ToInt32 as it rounds and my original'val' may have a sub-penny value that I want truncated (ie1.2357F should convert to 1230 Int32).
thanks