How to set precision to float point number on print in Python

1 Answer

0 votes
PI = 3.14159

print("PI (1) = ", PI)

print("PI (2) = %.2f" % PI)
print("PI (3) = %.3f" % PI)

print("PI (4) = ", round(PI, 2))
print("PI (5) = ", round(PI, 3))

print("PI (6) = {0:.2f}".format(PI))
print("PI (7) = {0:.3f}".format(PI))

print("PI (8) = ", format(round(PI, 2)))
print("PI (9) = ", format(round(PI, 3)))

'''
run:

PI (1) =  3.14159
PI (2) = 3.14
PI (3) = 3.142
PI (4) =  3.14
PI (5) =  3.142
PI (6) = 3.14
PI (7) = 3.142
PI (8) =  3.14
PI (9) =  3.142

'''

 



answered Feb 18, 2016 by avibootz
edited Feb 20, 2016 by avibootz

Related questions

1 answer 173 views
1 answer 172 views
2 answers 175 views
1 answer 160 views
1 answer 175 views
...