How to convert floating point number to an integer in Python

4 Answers

0 votes
f = 3.14

print(int(f))

   
   
   
'''
run:
   
3
   
'''

 



answered Apr 23, 2021 by avibootz
0 votes
import math
 
print(math.trunc(0))
print(math.trunc(1))
 
print(math.trunc(3.49))
print(math.trunc(3.5))
print(math.trunc(3.51))

print(math.trunc(-3.99))
 
    
    
    
'''
run:
    
0
1
3
3
3
-3
 
'''

 



answered Apr 23, 2021 by avibootz
0 votes
import math

print(math.ceil(0))
print(math.ceil(1))

print(math.ceil(3.01))
print(math.ceil(3.49))
print(math.ceil(3.5))
print(math.ceil(3.51))

print(math.ceil(-3.99))

   
   
   
'''
run:
   
0
1
4
4
4
4
-3

'''

 



answered Apr 23, 2021 by avibootz
0 votes
import math
 
print(math.floor(0))
print(math.floor(1))
 
print(math.floor(3.49))
print(math.floor(3.5))
print(math.floor(3.51))

print(math.floor(-3.99))
 
    
    
    
'''
run:
    
0
1
3
3
3
-4
 
'''

 



answered Apr 23, 2021 by avibootz
...