Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 743131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T08:49:39+00:00 2026-05-14T08:49:39+00:00

I want to represent a floating-point number as a string rounded to some number

  • 0

I want to represent a floating-point number as a string rounded to some number of significant digits, and never using the exponential format. Essentially, I want to display any floating-point number and make sure it “looks nice”.

There are several parts to this problem:

  • I need to be able to specify the
    number of significant digits.
  • The number of significant digits
    needs to be variable, which can’t be
    done with with the string formatting
    operator
    . [edit] I’ve been corrected; the string formatting operator can do this.
  • I need it to be rounded the way a
    person would expect, not something
    like 1.999999999999

I’ve figured out one way of doing this, though it looks like a work-round and it’s not quite perfect. (The maximum precision is 15 significant digits.)

>>> def f(number, sigfig):
    return ("%.15f" % (round(number, int(-1 * floor(log10(number)) + (sigfig - 1))))).rstrip("0").rstrip(".")

>>> print f(0.1, 1)
0.1
>>> print f(0.0000000000368568, 2)
0.000000000037
>>> print f(756867, 3)
757000

Is there a better way to do this? Why doesn’t Python have a built-in function for this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T08:49:39+00:00Added an answer on May 14, 2026 at 8:49 am

    It appears there is no built-in string formatting trick which allows you to (1) print floats whose first significant digit appears after the 15th decimal place and (2) not in scientific notation. So that leaves manual string manipulation.

    Below I use the decimal module to extract the decimal digits from the float.
    The float_to_decimal function is used to convert the float to a Decimal object. The obvious way decimal.Decimal(str(f)) is wrong because str(f) can lose significant digits.

    float_to_decimal was lifted from the decimal module’s documentation.

    Once the decimal digits are obtained as a tuple of ints, the code below does the obvious thing: chop off the desired number of sigificant digits, round up if necessary, join the digits together into a string, tack on a sign, place a decimal point and zeros to the left or right as appropriate.

    At the bottom you’ll find a few cases I used to test the f function.

    import decimal
    
    def float_to_decimal(f):
        # http://docs.python.org/library/decimal.html#decimal-faq
        "Convert a floating point number to a Decimal with no loss of information"
        n, d = f.as_integer_ratio()
        numerator, denominator = decimal.Decimal(n), decimal.Decimal(d)
        ctx = decimal.Context(prec=60)
        result = ctx.divide(numerator, denominator)
        while ctx.flags[decimal.Inexact]:
            ctx.flags[decimal.Inexact] = False
            ctx.prec *= 2
            result = ctx.divide(numerator, denominator)
        return result 
    
    def f(number, sigfig):
        # http://stackoverflow.com/questions/2663612/nicely-representing-a-floating-point-number-in-python/2663623#2663623
        assert(sigfig>0)
        try:
            d=decimal.Decimal(number)
        except TypeError:
            d=float_to_decimal(float(number))
        sign,digits,exponent=d.as_tuple()
        if len(digits) < sigfig:
            digits = list(digits)
            digits.extend([0] * (sigfig - len(digits)))    
        shift=d.adjusted()
        result=int(''.join(map(str,digits[:sigfig])))
        # Round the result
        if len(digits)>sigfig and digits[sigfig]>=5: result+=1
        result=list(str(result))
        # Rounding can change the length of result
        # If so, adjust shift
        shift+=len(result)-sigfig
        # reset len of result to sigfig
        result=result[:sigfig]
        if shift >= sigfig-1:
            # Tack more zeros on the end
            result+=['0']*(shift-sigfig+1)
        elif 0<=shift:
            # Place the decimal point in between digits
            result.insert(shift+1,'.')
        else:
            # Tack zeros on the front
            assert(shift<0)
            result=['0.']+['0']*(-shift-1)+result
        if sign:
            result.insert(0,'-')
        return ''.join(result)
    
    if __name__=='__main__':
        tests=[
            (0.1, 1, '0.1'),
            (0.0000000000368568, 2,'0.000000000037'),           
            (0.00000000000000000000368568, 2,'0.0000000000000000000037'),
            (756867, 3, '757000'),
            (-756867, 3, '-757000'),
            (-756867, 1, '-800000'),
            (0.0999999999999,1,'0.1'),
            (0.00999999999999,1,'0.01'),
            (0.00999999999999,2,'0.010'),
            (0.0099,2,'0.0099'),         
            (1.999999999999,1,'2'),
            (1.999999999999,2,'2.0'),           
            (34500000000000000000000, 17, '34500000000000000000000'),
            ('34500000000000000000000', 17, '34500000000000000000000'),  
            (756867, 7, '756867.0'),
            ]
    
        for number,sigfig,answer in tests:
            try:
                result=f(number,sigfig)
                assert(result==answer)
                print(result)
            except AssertionError:
                print('Error',number,sigfig,result,answer)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse a string that represent a DateTime in UTC format. My
Suppose I have a hex number 4072508200000000 and I want the floating point number
I want to represent some data visually, using a visualization component. Now I want
I want convert the storage of a floating point number to an integer (the
In SQL Server 2008, I want to represent an integer as a 3-character string
Let's say I have some code that does some floating point arithmetic and stores
I have some data in a tree structure, and I want to represent them
A Measured value consists of (typically nonnegative) floating-point number and unit-of-measure. The point is
I want to represent height in this format in javascript 6'00 I tried this
As (hopefully) most of you know, floating point arithmetic is different from real number

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.