home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The C Users' Group Library 1994 August
/
wc-cdrom-cusersgrouplibrary-1994-08.iso
/
listings
/
v_02_09
/
2n09044a
< prev
next >
Wrap
Text File
|
1991-07-23
|
659b
|
31 lines
/* FROUNDL Round float n to precision p, return
| ~~~~~~~ long = n * 10^p.
*/
#include <math.h>
long froundl(const double n, const int p)
{
long r;
r = (long)(n * pow(10.0, (double)(p+1)));
if ((r % 10L) > 4L)
r += 5L;
return(r / 10L);
}
void main() /* Test driver */
{
double val;
long result;
val = 3.141592654;
printf("Rounding the value: %10.8lf\n", val);
printf("to 2 places: %ld\n", froundl(val, 2));
printf("to 3 places: %ld\n", froundl(val, 3));
printf("to 4 places: %ld\n", froundl(val, 4));
printf("to 5 places: %ld\n", froundl(val, 5));
}