Simple atof() question

Noam Mendelson c60b-1eq at web-1b.berkeley.edu
Wed Mar 20 05:22:12 AEST 1991


In article <1214 at caslon.cs.arizona.edu> armstron at 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 <stdio.h>
>
>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 at web.Berkeley.EDU            |  it's backed up on tape
University of California at Berkeley |  somewhere."



More information about the Comp.lang.c mailing list