Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to make a variable change from the text "1m" into "1000000" in python

Tags:

python

numbers

I have variables with values like 1.7m 1.8k and 1.2b how can I convert them to a real number value for example

1.7m = 1700000
1.8k = 1800
1.2b = 1200000000
like image 549
Pevo Avatar asked Dec 02 '25 10:12

Pevo


2 Answers

I would define a dictionary:

tens = dict(k=10e3, m=10e6, b=10e9)

then

x='1.7m'
factor, exp = x[0:-1], x[-1].lower()
ans = int(float(factor) * tens[exp])
like image 123
Vicki Laidler Avatar answered Dec 05 '25 00:12

Vicki Laidler


You might be interested in a units library like quantities or unum.

like image 21
Mike Graham Avatar answered Dec 04 '25 23:12

Mike Graham



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!