How to extract the int part and the decimal part from float number in Python

3 Answers

0 votes
import math
 
f = 234.872
 
fraction, whole = math.modf(f)
 
print(whole)
print(fraction)
 
 
 
 
'''
run:
 
234.0
0.8720000000000141
 
'''

 



answered Aug 29, 2019 by avibootz
0 votes
import math
 
f = 234.872
 
whole = math.floor(f)   
fraction = f - whole     
 
print(whole)
print(fraction)
 
 
 
 
'''
run:
 
234.0
0.8720000000000141
 
'''

 



answered Aug 29, 2019 by avibootz
0 votes
f = 234.872
 
whole = int(f)   
fraction = f - int(f)     
 
print(whole)
print(fraction)
 
 
 
 
'''
run:
 
234
0.8720000000000141
 
'''

 



answered Aug 29, 2019 by avibootz

Related questions

...