Converting suffixes to integer

I've been working on something that scrapes the text off a site, compares it to the minimum and then prints the result. Here's what i'm working with:

class RobloxMessage:
def __init__(self):
    self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Firefox/52.0'}
    self.session = requests.Session()

def groupcheck(self, group_id):
    url = "" + group_id
    r = requests.get(url)
    if 'Funds:' in r.text:
        currency = r.text.split('<span class="robux" style="margin-left:5px">')[1].split('</')[0]
    if currency >= '50':
        owner = r.text.split('<a style="font-style: italic;" href="')[1].split('>')[1].split('<')[0]
    if owner == 'No One!':
            print(''+group_id +'\nCurrency: ' + currency + '$'

( yes i define what the variable 'group_id' is, gets random ids or the id that is chosen )

The currency has suffixes | ex: 1K, 1M, 1B, etc and I was wondering how you would convert it to a number without commas depending on its suffix. I tried everything, no solution so hopefully I can figure this out finally.

2 answers

  • answered 2018-03-11 12:58 Giorgi Jambazishvili

    depending on the information you posted, I suppose that the string may vary like: 1, 2.3K, 5.8M, 7B (examples). To cast this suffixes in normal value you may use the following code, which construct the dict of suffixes and it's coefficients, then if value contains one of the suffixes, then multiply value on corresponding coefficient:

    suffixes = {'K': 1000, 'M': 1000000, 'B': 1000000000}
    value = '3.2B' #  for example
    if value[-1] in suffixes.keys():
      value = float(value[:-1]) * suffixes.get(value[-1])

    hope it helps

  • answered 2018-03-11 12:58 johnashu

    Assuming all prefixes are at the end you can do the following using a dictionary of values.

    sizes_dict = {'B': 1, 'K': 1000, 'M': 1000000}
    data = '12M'
    for k, v in sizes_dict.items():
        if data[-1] == k:
            no_letters = int(data[:-1]) * v