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?

1
  • 4
    format(result, '.6f')? Commented Jan 23, 2016 at 21:45

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.