8

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

17

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Matthew
  • 7,440
  • 1
  • 24
  • 49