Mail Archives: djgpp/1997/06/06/11:24:54
On Sat, May 31, 1997 at 01:01:37AM +0000, POULAIN Vincent wrote:
>
>
> abcEd <abced AT pinnacle DOT com DOT au> a écrit dans l'article
> <01bc6dff$6cbac9c0$10ee13cb AT bbs>...
> > whats the best way of converting from float to int?
>
> I do use a simple way :
>
> int a;
> float b=3.14;
>
> a=b;
This is pretty sure an unportable way to do it. C is not FORTRAN.
Use casts a=(int)b; or use, what you need for your purpose:
#include <math.h>
main()
{
int a;
float b = 3.4,c = 3.5;
a = rint(b);
printf("rint(%f) = %d\n",b,a);
a = rint(c);
printf("rint(%f) = %d\n",c,a);
}
>
> Fine, isn't it ?
> Only if you want the integer part of your float.
> If you want a rounded value, this will not work of course :-)
>
> Vincent
>
--
--
Chris Christoph P. U. Kukulies kuku AT gil DOT physik DOT rwth-aachen DOT de
- Raw text -