58

I need to print or convert a float number to 15 decimal place string even if the result has many trailing 0s eg:

1.6 becomes 1.6000000000000000

I tried round(6.2,15) but it returns 6.2000000000000002 adding a rounding error

I also saw various people online who put the float into a string and then added trailing 0's manually but that seems bad...

What is the best way to do this?

2
  • 5
    Side note, but that last 2 is not a rounding error exactly. The number 6.2, like many other real numbers, is not exactly representable with a floating point variable in a computer. See docs.python.org/tutorial/floatingpoint.html and stackoverflow.com/questions/1089018/… for more information.
    – mtrw
    Dec 19, 2011 at 22:21
  • 3
    @mtrw, I think you could get away with calling it a rounding error - the input is being rounded to the closest binary number. Dec 19, 2011 at 22:42

5 Answers 5

82

For Python versions in 2.6+ and 3.x

You can use the str.format method. Examples:

>>> print('{0:.16f}'.format(1.6))
1.6000000000000001

>>> print('{0:.15f}'.format(1.6))
1.600000000000000

Note the 1 at the end of the first example is rounding error; it happens because exact representation of the decimal number 1.6 requires an infinite number binary digits. Since floating-point numbers have a finite number of bits, the number is rounded to a nearby, but not equal, value.

For Python versions prior to 2.6 (at least back to 2.0)

You can use the "modulo-formatting" syntax (this works for Python 2.6 and 2.7 too):

>>> print '%.16f' % 1.6
1.6000000000000001

>>> print '%.15f' % 1.6
1.600000000000000
7
  • 1
    +1. This answer works in >= Python 2.7. In Python 2.6 it would be '{0:.16f}'.format(x), not sure about older Python.
    – mtrw
    Dec 19, 2011 at 22:25
  • @mtrw Good point. The "modulo-formatting" (e.g., '%.16f' % 1.6) works at least back to Python 2.0 (Python 2.0 String Formatting Operations). Dec 19, 2011 at 22:37
  • i get a weird error when i try the above code: # Error: ValueError: zero length field name in format # Dec 19, 2011 at 22:44
  • @jonathantopf Which version of Python do you have? As @mtrw pointed out in a comment above, if you have Python 2.6, you need the positional argument (the 0 in the code of that comment). This is also true if you have Python 3.0. Dec 19, 2011 at 23:12
  • @mtrw I updated the answer to use the positional argument and include the "modulo-formatting" for pre-2.6 interpreters. Dec 19, 2011 at 23:30
16

The cleanest way in modern Python >=3.6, is to use an f-string with string formatting:

>>> var = 1.6
>>> f"{var:.15f}"
'1.600000000000000'
0
5

Floating point numbers lack precision to accurately represent "1.6" out to that many decimal places. The rounding errors are real. Your number is not actually 1.6.

Check out: http://docs.python.org/library/decimal.html

3

I guess this is essentially putting it in a string, but this avoids the rounding error:

import decimal

def display(x):
    digits = 15
    temp = str(decimal.Decimal(str(x) + '0' * digits))
    return temp[:temp.find('.') + digits + 1]
3

We can use format() to print digits after the decimal places. Taken from http://docs.python.org/tutorial/floatingpoint.html

>>> format(math.pi, '.12g')  # give 12 significant digits
'3.14159265359'

>>> format(math.pi, '.2f')   # give 2 digits after the point
'3.14'
1
  • 1
    It's not true that '.12g' always gives 12 significant digits: >>> f'{1.23456:.12g}' '1.23456'
    – KeithB
    Sep 22, 2022 at 14:49

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.