home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_300 / 344_02 / rndnick.c < prev    next >
Text File  |  1990-05-20  |  2KB  |  58 lines

  1. /*
  2. HEADER:         ;
  3. TITLE:          round to nearest nickel;
  4. VERSION:        1.0;
  5.  
  6. DESCRIPTION:    function rounds value to nearest nickel
  7.  
  8. KEYWORDS:       rouding, nickel;
  9. SYSTEM:         Xenix 3.4b, MSDOS;
  10. FILENAME:       rndnick.c
  11. WARNINGS:       compile with -dNO_PROTOTYPE if your system does not
  12.                 support prototyping, with -dFOR_MSDOS if you are compiling
  13.                 for MSDOS with an ANSI standard compiler.
  14.                 Defaults assume compiling with prototypes for
  15.                 Xenix 3.4b on Altos 2086 computer.
  16.  
  17. SEE-ALSO:       vernmath.h
  18. AUTHORS:        Vern Martin, 449 W. Harrison, Alliance, Ohio 44601;
  19. COMPILERS:      ECOSOFT ECO-C88, XENIX 3.4B STANDARD COMPILER;
  20. */
  21.  
  22. #include "vernmath.h"
  23.  
  24. #define ROUND_TO_NEAREST    (5.0)   /* round to neareast 5 cents */
  25. #define CUT_OFF             (3)     /* less than this and round down,
  26.                                         greater or ==, and round up */
  27.  
  28. double rndnick(amt)
  29. double amt; /* amount to round */
  30. {
  31.     double remainder;   /* remainder after modulus operation */
  32.  
  33.     /* multiply amount by 100 and then throw away anything
  34.         that is right of the decimal */
  35.     /* then perform modulus arithmetic to find out what remainder
  36.         is afger deviding by the ROUND_TO_NEAREST factor (nickel) */
  37.  
  38.  
  39.     amt = nrnd54(amt,2) * 100;
  40.  
  41.     /* round output of fmod(), it is not an exact number and must be ! */
  42.     remainder = nrnd54(fmod(amt,ROUND_TO_NEAREST),0);
  43.  
  44.     /* now, if remainder is less than the cut off, subtract it from
  45.         the amount, else add the difference between the remainder
  46.         and the cut off, thus rounding up to the nearest nickel */
  47.  
  48.     if ( (int) remainder < CUT_OFF )  {
  49.         amt -= remainder;
  50.     }
  51.     else {
  52.         amt += ROUND_TO_NEAREST - remainder;
  53.     }
  54.  
  55.     return( amt * .01 );
  56.  
  57. }
  58.