problem float to int

I have a problem with the conversion from float to int.

float FAux;
int VMin;

strcpy(ss,“27.9”);
FAux=atof(ss); //FAux=27.9
if(_xtoxErr) break;
FAux=FAux*10; //FAux=279
VMin=(int)FAux; //VMin=278 Why?

Why is there 278 into VMin?

I have Dinamic C 9.52.

Thanks

[QUOTE=sgianty;1168]I have a problem with the conversion from float to int.

float FAux;
int VMin;

strcpy(ss,“27.9”);
FAux=atof(ss); //FAux=27.9
if(_xtoxErr) break;
FAux=FAux*10; //FAux=279
VMin=(int)FAux; //VMin=278 Why?

Why is there 278 into VMin?

I have Dinamic C 9.52.

[/quote]

Floating point math has limited precision, especially single precision floating point. There are many numbers that cannot be truly represented in floating point format (however they are generally accurate enough - but NOT in your particular context). Simple fix for positive numbers:

VMin = (int) (FAux + 0.5);

If you want to handle negative numbers too as well you’ll want to test the
sign and add or subtract 0.5 accordingly. Good luck!

P.S.- To answer your question WHY it came out 278 it’s because 27.9 was actually represented by 27.899999619 and when multiplied by 10.0 came out to 278.99999619 and the float to int conversion stopped when it hit the decimal point.