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 >
Text File  |  1991-07-23  |  659b  |  31 lines

  1.  
  2. /*  FROUNDL  Round float n to precision p, return
  3.  |  ~~~~~~~  long = n * 10^p.
  4.  */
  5.  
  6. #include  <math.h>
  7.  
  8. long froundl(const double n, const int p)
  9. {
  10.     long r;
  11.  
  12.     r = (long)(n * pow(10.0, (double)(p+1)));
  13.     if  ((r % 10L) > 4L)
  14.         r += 5L;
  15.     return(r / 10L);
  16. }
  17.  
  18. void main()     /* Test driver */
  19. {
  20.     double val;
  21.     long result;
  22.     
  23.     val = 3.141592654;
  24.     printf("Rounding the value: %10.8lf\n", val);
  25.  
  26.     printf("to 2 places: %ld\n", froundl(val, 2));
  27.     printf("to 3 places: %ld\n", froundl(val, 3));
  28.     printf("to 4 places: %ld\n", froundl(val, 4));
  29.     printf("to 5 places: %ld\n", froundl(val, 5));
  30. }
  31.