home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #30 / NN_1992_30.iso / spool / comp / lang / c / 18324 < prev    next >
Encoding:
Text File  |  1992-12-14  |  3.2 KB  |  80 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!spool.mu.edu!yale.edu!yale!gumby!destroyer!ncar!earl.scd.ucar.edu!ethan
  3. From: ethan@earl.scd.ucar.edu (Ethan Alpert)
  4. Subject: Re: Rounding floats to X significant digits
  5. Message-ID: <1992Dec14.171533.20032@ncar.ucar.edu>
  6. Followup-To: comp.lang.c 
  7. Summary: Your response doesn't work for numbers > 1.0
  8. Keywords: rounding float significant digits
  9. Sender: ethan@ncar.ucar.edu
  10. Organization: Scientific Computing Divison/NCAR Boulder, CO
  11. References: <1992Dec11.171533.2643@ncar.ucar.edu> <1992Dec11.172414.3449@ncar.ucar.edu> <stanleyr.724176076@kramden>
  12. Distribution: usa
  13. Date: Mon, 14 Dec 1992 17:15:33 GMT
  14. Lines: 64
  15.  
  16. In article <stanleyr.724176076@kramden> stanleyr@kramden.nyu.edu (Rick Stanley) writes:
  17. >
  18. >ethan@earl.scd.ucar.edu (Ethan Alpert) writes:
  19. >>of significant digits, decimal places.
  20. >
  21. >
  22. >How about the following, which works well for me.
  23.  
  24. The following doesn't work for large numbers only numbers with digits after
  25. the decimal point. For example, take the number 1,325,160. If I wan't
  26. to round this to 3 significant digits the answer would be 1,330,000. The
  27. code below would return 1,325,160 which is incorect.
  28.  
  29. -ethan 
  30.  
  31. >
  32. >/* -----------------------------------------------------------------------
  33. >FILE: Round.cpp          AUTHOR: Rick Stanley             CREATED: 10/10/91
  34. >DESCRIPTION: Round a double number to n decimal places.  for use in rounding
  35. >off dollar amounts to 2 decimal places for increased accuracy in calculations
  36. >and display.  I can't trust the accuracy of printf() using floats.
  37. >--------------------------------------------------------------------------
  38. >Copyright (C) 1991, Rick Stanley (212) 569-9304  All Rights Reserved
  39. >-------------------------------------------------------------------        */
  40. >#include <stdio.h>
  41. >#include <math.h>
  42. >double round(double value, int precision);
  43. >double round(double value, int precision)
  44. >{
  45. >    double result, power;
  46. >    long ltemp;
  47. >    power = pow(10.0, (double) precision);
  48. >    result = (value * power + .5);
  49. >    ltemp = (long) result;
  50. >    result = ((double) ltemp) / power;
  51. >    return result;
  52. >}
  53. >
  54. >/* Where precision is the number of decimal places to round for,
  55. >   dollars = round(dollars, 2); to round to 2 decimal places.  */
  56. >
  57. >I've seen this reduced to one line of code, but prefer to do it this way.
  58. >I believe this will work using negative precision thus rounding the
  59. >left side of the decimal point.  I have not used it that way.  Test it!
  60. >
  61. >-------------------------------------+-----------------------------------
  62. >Rick Stanley                         | R.S. Consultants  (212) 569-9304
  63. >stanleyr@acf14.nyu.edu               | 
  64. >Information Technologies Institute   |        C++ & C Language
  65. >New York University   NYC, NY  USA   | Programming, Training, Consulting
  66. >-------------------------------------+-----------------------------------
  67. >
  68. >
  69.  
  70.  
  71. --
  72. Ethan Alpert  internet: ethan@ncar.ucar.edu | Standard Disclaimer:
  73. Scientific Visualization Group,             | I represent myself only.
  74. Scientific Computing Division               |-------------------------------
  75. National Center for Atmospheric Research, PO BOX 3000, Boulder Co, 80307-3000
  76.