home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!spool.mu.edu!yale.edu!yale!gumby!destroyer!ncar!earl.scd.ucar.edu!ethan
- From: ethan@earl.scd.ucar.edu (Ethan Alpert)
- Subject: Re: Rounding floats to X significant digits
- Message-ID: <1992Dec14.171533.20032@ncar.ucar.edu>
- Followup-To: comp.lang.c
- Summary: Your response doesn't work for numbers > 1.0
- Keywords: rounding float significant digits
- Sender: ethan@ncar.ucar.edu
- Organization: Scientific Computing Divison/NCAR Boulder, CO
- References: <1992Dec11.171533.2643@ncar.ucar.edu> <1992Dec11.172414.3449@ncar.ucar.edu> <stanleyr.724176076@kramden>
- Distribution: usa
- Date: Mon, 14 Dec 1992 17:15:33 GMT
- Lines: 64
-
- In article <stanleyr.724176076@kramden> stanleyr@kramden.nyu.edu (Rick Stanley) writes:
- >
- >ethan@earl.scd.ucar.edu (Ethan Alpert) writes:
- >
- >>of significant digits, decimal places.
- >
- >
- >How about the following, which works well for me.
-
- The following doesn't work for large numbers only numbers with digits after
- the decimal point. For example, take the number 1,325,160. If I wan't
- to round this to 3 significant digits the answer would be 1,330,000. The
- code below would return 1,325,160 which is incorect.
-
- -ethan
-
- >
- >/* -----------------------------------------------------------------------
- >FILE: Round.cpp AUTHOR: Rick Stanley CREATED: 10/10/91
- >DESCRIPTION: Round a double number to n decimal places. for use in rounding
- >off dollar amounts to 2 decimal places for increased accuracy in calculations
- >and display. I can't trust the accuracy of printf() using floats.
- >--------------------------------------------------------------------------
- >Copyright (C) 1991, Rick Stanley (212) 569-9304 All Rights Reserved
- >------------------------------------------------------------------- */
- >#include <stdio.h>
- >#include <math.h>
- >
- >double round(double value, int precision);
- >
- >double round(double value, int precision)
- >{
- > double result, power;
- > long ltemp;
- > power = pow(10.0, (double) precision);
- > result = (value * power + .5);
- > ltemp = (long) result;
- > result = ((double) ltemp) / power;
- >
- > return result;
- >}
- >
- >/* Where precision is the number of decimal places to round for,
- > dollars = round(dollars, 2); to round to 2 decimal places. */
- >
- >I've seen this reduced to one line of code, but prefer to do it this way.
- >I believe this will work using negative precision thus rounding the
- >left side of the decimal point. I have not used it that way. Test it!
- >
- >-------------------------------------+-----------------------------------
- >Rick Stanley | R.S. Consultants (212) 569-9304
- >stanleyr@acf14.nyu.edu |
- >Information Technologies Institute | C++ & C Language
- >New York University NYC, NY USA | Programming, Training, Consulting
- >-------------------------------------+-----------------------------------
- >
- >
-
-
- --
- Ethan Alpert internet: ethan@ncar.ucar.edu | Standard Disclaimer:
- Scientific Visualization Group, | I represent myself only.
- Scientific Computing Division |-------------------------------
- National Center for Atmospheric Research, PO BOX 3000, Boulder Co, 80307-3000
-