I was trying to convert strings like:
s = '1m'
s = '1.4m'
s = '1k'
s = '1.4k'
To what their actual integer is (e.g '1k'
to 1000
),
And I want a function that I can call with:
print(tonum('1m'))
print(tonum('1.4m'))
print(tonum('1.45m'))
print(tonum('1k'))
print(tonum('1.4k'))
print(tonum('1.45k'))
Output:
1000000
1400000
1450000
1000
1400
1450
I tried:
def tonum(s):
if '.' in s:
return int(s.replace('.', '').replace('m', '00000').replace('k', '00'))
else:
return int(s.replace('m', '000000').replace('k', '000'))
But it works only for 1.4k
1k
1.4m
1m
, but not 1.45m
.