Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,885 questions

51,811 answers

573 users

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 152 views
1 answer 157 views
2 answers 160 views
1 answer 147 views
1 answer 155 views
...