How to get the first two digits of float number in Python

2 Answers

0 votes
f = 234.872
 
first_two_digits = int(str(f)[:2])
 
print(first_two_digits)
  
 
 
'''
run:
 
23
 
'''

 



answered Aug 29, 2019 by avibootz
edited Aug 29, 2019 by avibootz
0 votes
import math 

f = 234.872
 
first_two_digits = f // 10 ** (int(math.log(f, 10)) - 1)
 
print(first_two_digits)
  
 
 
'''
run:
 
23.0
 
'''

 



answered Aug 29, 2019 by avibootz
...