Click here to Skip to main content
13,145,794 members (59,595 online)
Click here to Skip to main content
Add your own
alternative version


20 bookmarked
Posted 5 Dec 2002

A Strategy for Displaying Numeric Data with Significant Figures

, 5 Dec 2002
Rate this:
Please Sign up or sign in to vote.
A strategy for displaying numeric data with significant figures using Log10.


Problem: Some times numeric data, for example floats and doubles, needs to be displayed with a certain amount of decimal precision and at times with a given amount of significant figures.

For example:

4.5568 displayed with 2 Significant Figures is: 4.6
0.0221 displayed with 3 Significant Figures is: 0.022
5.9 displayed with 1 Significant Figures is: 6

Strategy: Using the log10 function we can calculate the number of digits for the integer part of the number we are manipulating. The decimal part of the number can be displayed properly with a sprintf call or something similar as we can see in our function below. This solution also rounds the final number, which is usually desirable when displaying significant figures.

double CalculateSigFig(double num, int sigfig)
    char buff[128];//temp buffer
    double temp = 0; 
    int lognum = 0;
    std::string formatbuff;//format string

    if(sigfig <= 0 || num == 0)//damage control
        return num;

    temp = int(num);
    if(temp * -1 > 0)//check for negative numbers
        temp *= -1;

    lognum = (int)log10(temp);//get significant digits of integer part of num
    ++lognum; //adjust for log10 results
    sigfig -= lognum;//subtract for decimal precision calculation

    if(sigfig < 0)//check for negative results
        sigfig = 0;

    //construct format string
    formatbuff = "%";
    itoa(lognum, buff, 10);//convert lognum to int
    formatbuff += buff;
    formatbuff += ".";
    itoa(sigfig, buff, 10);
    formatbuff += buff;
    formatbuff += "f";
    //end construct format string

    //use format string to get desired results
    sprintf(buff, formatbuff.c_str(), num);

    return atof(buff);//return double converted from string


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Bryan Brown
United States United States
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralC# Version of Significant Figures or Significant Digits Pin
Leonard Lee20-Apr-08 21:26
memberLeonard Lee20-Apr-08 21:26 
QuestionAnother strategy ? Pin
Stlan9-Dec-02 3:29
memberStlan9-Dec-02 3:29 
General_fcvt Pin
Tim Smith7-Dec-02 2:41
memberTim Smith7-Dec-02 2:41 
GeneralRe: _fcvt Pin
zarzor7-Dec-02 11:20
memberzarzor7-Dec-02 11:20 
GeneralOr what... Pin
zarzor6-Dec-02 13:22
memberzarzor6-Dec-02 13:22 
QuestionWhat does that give me over... Pin
Nitron6-Dec-02 10:33
memberNitron6-Dec-02 10:33 
GeneralDoesn't work Pin
KevinHall6-Dec-02 6:19
memberKevinHall6-Dec-02 6:19 
QuestionDoes it work? Pin
eddie.breeveld6-Dec-02 5:20
membereddie.breeveld6-Dec-02 5:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170915.1 | Last Updated 6 Dec 2002
Article Copyright 2002 by Bryan Brown
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid