How to round a number to 1 decimal place in Python

4 Answers

0 votes
print(round(5.572, 1))  

print(round(5.513, 1)) 

     
     
'''
run:
 
5.6
5.5
 
'''

 



answered Jul 15, 2022 by avibootz
0 votes
import math

def round_down_float_to_1_decimal_place(num):
    return math.floor(num * 10) / 10
    
print(round_down_float_to_1_decimal_place(5.572))  

print(round_down_float_to_1_decimal_place(5.493)) 

     
     
'''
run:
 
5.5
5.4
 
'''

 



answered Jul 15, 2022 by avibootz
0 votes
import math

def round_up_float_to_1_decimal_place(num):
    return math.ceil(num * 10) / 10
    
print(round_up_float_to_1_decimal_place(5.513))  

print(round_up_float_to_1_decimal_place(5.426)) 

     
     
'''
run:
 
5.6
5.5
 
'''

 



answered Jul 15, 2022 by avibootz
0 votes
print("{:.1f}".format(5.593)) 

print("{:.1f}".format(5.426)) 


     
     
'''
run:
 
5.6
5.4
 
'''

 



answered Jul 15, 2022 by avibootz

Related questions

2 answers 138 views
2 answers 152 views
2 answers 140 views
4 answers 251 views
1 answer 158 views
...