Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print double in Python with exact precision? [duplicate]

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?

like image 828
ALEXANDER KONSTANTINOV Avatar asked Jan 23 '16 21:01

ALEXANDER KONSTANTINOV


1 Answers

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.

like image 129
Matthew Avatar answered Sep 23 '22 02:09

Matthew