Rounding in C

Douglas M. MacFarlane RHMCSUPV at MIAMIU.BITNET
Wed Sep 5 14:37:25 AEST 1990


Thanks to all who posted responses and e-mailed me directly on my
assinging values to type float question.  They were most appreciated.
 
Microsoft C ver 5.1 required an explicit cast (ugh or yuk from some
of you) i.e.:
 
fValue = (float) 35 ;
 
to remove the data conversion warning.
 
Next question:  Rounding
 
fPrice = 1.35 ;
 
fSpending = 10000 * fPrice ;
 
printf ( "Spending is:  %f", fSpending ) ;
 
produces the output 13499.9997  instead of 13500
 
I seem to recall that these problems are inherent to computing, but
a rounding function would do the trick.  Has anyone written a
RoundAFloat function that accepts a floating point value and the
number of decimal places to round to ( 2 for round to 2 decimal places,
-2 to round to the hundreds, etc.)?
 
If so, could you send it to me, or post.
 
 
Thanks
 
Doug MacFarlane
RHMCSUPV at miamiu.acs.muohio.edu



More information about the Comp.lang.c mailing list