home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!gatech!news.ans.net!cmcl2!kramden!stanleyr
- From: stanleyr@kramden.nyu.edu (Rick Stanley)
- Subject: Re: Rounding floats to X significant digits
- Message-ID: <stanleyr.724176076@kramden>
- Keywords: rounding float significant digits
- Sender: notes@cmcl2.nyu.edu (Notes Person)
- Nntp-Posting-Host: kramden.acf.nyu.edu
- Organization: New York University
- References: <1992Dec11.171533.2643@ncar.ucar.edu> <1992Dec11.172414.3449@ncar.ucar.edu>
- Distribution: usa
- Date: Sat, 12 Dec 1992 16:01:16 GMT
- Lines: 86
-
-
- ethan@earl.scd.ucar.edu (Ethan Alpert) writes:
-
- >I apologize if this appears in a FAQ.
-
- >I was wondering if anyone has any interesting techniques for rounding
- >floating point numbers to a certain number of significant digits?
-
- >I'm not talking a certain number of characters but a certain number
- >of significant digits, decimal places.
-
- >There doesn't appear to be a standard C library routine for this.
-
- >Sprintf only does it for digits *left* of the decimal place and not
- >for the entire number.
-
- >Here's what I came up with and I'm still not sure it works.
-
- >There has to be a better way to do it.
-
- >#include <stdio.h>
- >#include <math.h>
-
- >float roundit(float a, int sig_digit)
- >{
- > char buf[20];
- > char format[20];
- > float exp;
- > float tmp1;
- >
-
- > sprintf(format,"%%.%df",sig_digit);
- > if(a > 1.0) {
- > exp = (float)log10((double)a);
- > exp = (float)ceil((double)exp);
- > tmp1 = (float)pow((double)10.0,(double)exp);
- > sprintf(buf,format,a/tmp1);
- > return((float)atof(buf)*tmp1);
- > } else {
- > sprintf(buf,format,a);
- > return((float)atof(buf));
- > }
- >}
-
- How about the following, which works well for me.
-
- /* -----------------------------------------------------------------------
- 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
- -------------------------------------+-----------------------------------
-
-
-