How to display a float to 2 decimal places in Python

4 Answers

0 votes
n = 0.314

n = round(n, 2)

print(n)

'''
run:

0.31

'''

 



answered Jun 2, 2015 by avibootz
edited Jun 3, 2015 by avibootz
0 votes
n = 0.314

print("%.2f" % n)

'''
run:

0.31

'''

 



answered Jun 2, 2015 by avibootz
edited Jun 3, 2015 by avibootz
0 votes
n = 0.3554

n = round(n, 2)

print(n)

'''
run:

0.36

'''

 



answered Jun 2, 2015 by avibootz
edited Jun 3, 2015 by avibootz
0 votes
n = 0.314

print("{0:.2f}".format(n))

'''
run:

0.31

'''

 



answered Jun 2, 2015 by avibootz
edited Jun 3, 2015 by avibootz

Related questions

4 answers 437 views
4 answers 407 views
2 answers 374 views
2 answers 404 views
2 answers 199 views
1 answer 158 views
1 answer 205 views
...