Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!uwm.edu!bionet!agate!web-1b.berkeley.edu!c60b-1eq From: c60b-1eq@web-1b.berkeley.edu (Noam Mendelson) Newsgroups: comp.lang.c Subject: Re: Simple atof() question Message-ID: <1991Mar19.192212.14868@agate.berkeley.edu> Date: 19 Mar 91 19:22:12 GMT References: <1214@caslon.cs.arizona.edu> Sender: usenet@agate.berkeley.edu (USENET Administrator) Distribution: na Organization: University of California, Berkeley Lines: 30 In article <1214@caslon.cs.arizona.edu> armstron@cs.arizona.edu (Jim Armstrong) writes: > >How can I get atof() to return a float instead of a double? >When I run this simple program I get n = 37.549999 instead of >n = 37.55. What am I doing wrong? > > >#include > >main() >{ > >double atof(); >char buf[10]; >float n; > >strcpy(buf,"37.55"); >n = (float) atof(buf); >printf("n = %f\n",n); > >} atof() _is_ returning a float. It is a different matter if you would like to round it off to two decimal places. In that case you might want to try floor(x*100)/100. =============================================================== Noam Mendelson | "I haven't lost my mind, c60b-1eq@web.Berkeley.EDU | it's backed up on tape University of California at Berkeley | somewhere."