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 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit In-Reply-To: <01bc6d5d$4afb4aa0$LocalHost@vpoulain>; from POULAIN Vincent on Sat, May 31, 1997 at 01:01:37AM +0000 Precedence: bulk On Sat, May 31, 1997 at 01:01:37AM +0000, POULAIN Vincent wrote: > > > abcEd 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 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