How to round a number to 2 decimal places in Python

4 Answers

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

print("{:.2f}".format(5.4823)) 


     
     
'''
run:
 
5.57
5.48
 
'''

 



answered Jul 15, 2022 by avibootz
0 votes
print(round(5.5691, 2))  
 
print(round(5.4823, 2)) 
 


     
     
'''
run:
 
5.57
5.48
 
'''

 



answered Jul 15, 2022 by avibootz
0 votes
import math
 
def round_down_float_to_2_decimal_places(num):
    return math.floor(num * 100) / 100
     
print(round_down_float_to_2_decimal_places(5.5691))  
 
print(round_down_float_to_2_decimal_places(5.4823)) 



     
     
'''
run:
 
5.56
5.48
 
'''

 



answered Jul 15, 2022 by avibootz
0 votes
import math
 
def round_up_float_to_2_decimal_places(num):
    return math.ceil(num * 100) / 100
     
print(round_up_float_to_2_decimal_places(5.5691))  
 
print(round_up_float_to_2_decimal_places(5.4123)) 
 



     
     
'''
run:
 
5.57
5.42
 
'''

 



answered Jul 15, 2022 by avibootz

Related questions

1 answer 158 views
1 answer 257 views
3 answers 217 views
2 answers 163 views
1 answer 107 views
3 answers 225 views
...