float to int function?
Hello!
I made a function that converts float to int by 1000 * float and something very strange happen, not all the time, but after some period. Here is the function, if u know something better...
int CAppForm::ftoi(float float_in)
{
int nr_integer = int(float_in*1000);
return nr_integer;
}
Conversion of ftoi(0.450) should be 450, but it give me 449, (not all the time, like i said, but after a period), give me some clues!
Thanks!!!
[490 byte] By [
albusorin] at [2007-11-18 17:10:13]

# 1 Re: float to int function?
Originally posted by albusorin
and something very strange happenWill you tell us what - or should we guess? :)
# 2 Re: float to int function?
Added by albusorin
Conversion of ftoi(0.450) should be 450, but it give me 449, (not all the time, like i said, but after a period), give me some clues!Looks like the float you are passing isn't exactly 0.450, but something like 4.49999... This happens easily when doing float math. How did you assert that your float value is actually 0.45? Also note that your function will truncate the float value to an integer, it doesn't perform any rounding. Is that really what you want?
# 3 Re: float to int function?
To perform rounding i used ceil function, wich actually works very good. Thanks gstercken!!!
int ftoi(float f_in)
{
float temp = (f_in*1000);
int ple = (int(ceil(temp)));
return ple;
}
# 4 Re: float to int function?
Originally posted by albusorin
To perform rounding i used ceil function, wich actually works very good. Thanks gstercken!!!ceil can't work - it will not round, but return the next integer that is grater or equal to your float. For example, 4.55 will become 5, but 4.2 will become 5 too (which is wrong - it should be 4).
# 5 Re: float to int function?
So, it means that float to int can't be done the simple way, NO?!? I think that it could be a way to resolve this:
int CAppForm::ftoi(float f_in)
{
int num;
CString str;
str.Format("%.0f",f_in * 1000);
num = atoi(str);
return num;
}
//this function I use for conversion from centimeters to milimeters
I know that gstercken will say something about this :D, but no one can give me somthing that works...
# 6 Re: float to int function?
int nr_integer = (int) floor(float_in*1000+0.5);
might do
# 7 Re: float to int function?
Originally posted by Simon666
int nr_integer = (int) floor(float_in*1000+0.5);
might do The floor() function can be done away, as the deicmal part will be truncated away.
# 8 Re: float to int function?
Originally posted by CBasicNet
The floor() function can be done away, as the deicmal part will be truncated away.
That won't round.
# 9 Re: float to int function?
Huh? When you add 0.5, you already take care of the rounding.
# 10 Re: float to int function?
Originally posted by CBasicNet
Huh? When you add 0.5, you already take care of the rounding.
Never thought of that, you mean leave the +0.5 in place like this? That seems correct as well.
int nr_integer = (int) (float_in*1000+0.5);
# 11 Re: float to int function?
Yes.