Skip to content

Instantly share code, notes, and snippets.

@asadamatic
Forked from gajeshbhat/strToInt.py
Last active February 16, 2020 07:51
Show Gist options
  • Save asadamatic/a0077b28369f5fbe7539cdbac898cc74 to your computer and use it in GitHub Desktop.
Save asadamatic/a0077b28369f5fbe7539cdbac898cc74 to your computer and use it in GitHub Desktop.
Convert k to Integer thousand Python.
def convert_si_to_number(x):
total_stars = 0
if 'k' in x:
if len(x) > 1:
total_stars = float(x.replace('k', '')) * 1000 # convert k to a thousand
elif 'M' in x:
if len(x) > 1:
total_stars = float(x.replace('M', '')) * 1000000 # convert M to a million
elif 'B' in x:
total_stars = float(x.replace('B', '')) * 1000000000 # convert B to a Billion
elif ',' in x:
total_stars = float(x.replace(',','')) # removing ,
else:
total_stars = int(x) # Less than 1000
return int(total_stars)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment