How do I print double in Python with exact precision?

34,927

Try using a format string:

print("%.6f"%4.0) # 4.000000

Or alternatively:

print("{:.6f}".format(4.0))

See the Python documentation for details on format strings and more examples.

Share:
34,927
ALEXANDER KONSTANTINOV
Author by

ALEXANDER KONSTANTINOV

Updated on July 09, 2022

Comments

  • ALEXANDER KONSTANTINOV
    ALEXANDER KONSTANTINOV almost 2 years

    I need to print double with precision equal exactly to 6, I found function round:

    print(str(round(result, 6))
    

    But in case result itself has less precision, the print function skips zeros at the end.

    Gor example, the output of such code,

    print(str(round(4.0, 6)))
    

    is

    4.0
    

    But what I need is

    4.000000
    

    How can I reach this?