Python – round, limit decimal places

Posted by on Sep 16, 2010 in code, python | No Comments
pi = 3.14159265

print(pi)
# Result: 3.14159265 #

print("%.2f" % pi)
# Result: 3.14 #

round(pi,3)
# Result: 3.142 # 

print("%.1f" % round(pi,0))
# Result: 3.0 # 

Email this to someoneTweet about this on TwitterShare on FacebookShare on Google+Share on LinkedInShare on StumbleUponShare on RedditDigg this

Leave a Reply