delorie.com/archives/browse.cgi search
Mail Archives: djgpp/1997/06/06/11:24:54

 Message-ID: <19970606172546.00228@gil.physik.rwth-aachen.de> Date: Fri, 6 Jun 1997 17:25:46 +0200 From: Christoph Kukulies To: POULAIN Vincent Cc: djgpp AT delorie DOT com Subject: Re: how to convert float ->int? References: <01bc6dff\$6cbac9c0\$10ee13cb AT bbs> <01bc6d5d\$4afb4aa0\$LocalHost AT vpoulain> Mime-Version: 1.0 In-Reply-To: <01bc6d5d\$4afb4aa0\$LocalHost@vpoulain>; from POULAIN Vincent on Sat, May 31, 1997 at 01:01:37AM +0000

```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 -

 webmaster delorie software   privacy Copyright © 2019   by DJ Delorie Updated Jul 2019