0

I am using following code to typecast from float to int. I always have float with up to 1 decimal point. First I multiply it with 10 and then typecast it to int

float temp1 = float.Parse(textBox.Text);
int temp = (int)(temp1*10);

for 25.3 I get 252 after typecasting but for 25.2, 25.4 I get correct output 252,254 respectively.

Now performing same operation little bit in different way gives correct output.

float temp1 = float.Parse(textBox.Text);
temp1 = temp1*10;
int temp = (int)temp1;

now for 25.3 I get 253. What is the reason for this because logically first method is also correct? I am using Visual Studio 2010.

prattom
  • 1,625
  • 11
  • 42
  • 67
  • 2
    Surely you did not write `float temp1 = 25.3`? That wouldn't compile. – harold Jan 10 '15 at 12:03
  • 25.3 can't be represented exactly in binary, so it is rounded to the nearest number, which is probably a bit less. Converting to int just truncates the digits behind the radix separator. – Ulrich Eckhardt Jan 10 '15 at 12:03
  • 2
    Also, this happens for x86 but not for x64, which explains why putting it in a variable mattered. The x86 JIT uses FPU instructions, which are more precise than the type of the variable, so just the fact that you store it changes the value. – harold Jan 10 '15 at 12:06
  • Sorry my mistake actually I was reading temp1 from c# form so it is float.Parse(textBox.Text) – prattom Jan 10 '15 at 12:11
  • The (int) cast **truncates** the floating point value. So 252.99999 (the actual value) is truncated to 25.2 You must round, `int temp = (int)(temp1*10 + 0.5);` – Hans Passant Jan 10 '15 at 12:40
  • @harold Has the correct answer for this question. When just doing the multiplication and then converting it, the number is held in FPU registers. The act of storing it back into the variable and then using the variable content afterwards looses precision. – Lasse V. Karlsen Jan 10 '15 at 13:01
  • But why 25.2 is 252 then not 251 according to first step. – prattom Jan 10 '15 at 13:16
  • @HansPassant, that's not correct rounding when applied to negative numbers. There is a standard round() function for that. – Ulrich Eckhardt Jan 11 '15 at 19:17

3 Answers3

0

Its all because of precision in float & double & there rounding off to integer

By default arithmetic operation are performed to in double precision

here is your first code

float temp1 = float.Parse(textBox.Text);
int temp = (int)(temp1*10);

which gets executed as

float temp1 = float.Parse(textBox.Text);
double xVariable = temp1*10
int temp = (int)xVariable;

here is your second code which executes as float conversion of multiplication

float temp1 = float.Parse(textBox.Text);
float xVariable = temp1*10;
int temp = (int)xVariable;

More Information about precision

http://en.wikipedia.org/wiki/Single-precision_floating-point_format

What range of numbers can be represented in a 16-, 32- and 64-bit IEEE-754 systems?

Community
  • 1
  • 1
Sangram Chavan
  • 331
  • 1
  • 9
0

try

decimal temp1 = decimal.Parse(textBox.Text);
int temp = (int)(temp1*10);
masaki
  • 121
  • 1
  • 6
-3

Use this:

float temp1 = 25.3;
int temp = Int32. conversation (temp1)
fejese
  • 4,601
  • 4
  • 29
  • 36