(n, limit=5, sig_figures=None, decimal_units=True)
Set a number over a certain length to something shorter.
    For example, 2000000 can be shortened to 2m.
    The numbers will be kept as long as possible, 
    so "2000k" will override "2m" at a length of 5.
    Set a minimum length to ensure it has a certain number of digits.
    Disable decimal_units if you do not want this enforced on units (such as 15.000000).
Set a number over a certain length to something shorter.
    For example, 2000000 can be shortened to 2m.
    The numbers will be kept as long as possible, 
    so "2000k" will override "2m" at a length of 5.
    Set a minimum length to ensure it has a certain number of digits.
    Disable decimal_units if you do not want this enforced on units (such as 15.000000).
Set a number over a certain length to something shorter . For example 2000000 can be shortened to 2m . The numbers will be kept as long as possible so 2000k will override 2m at a length of 5 . Set a minimum length to ensure it has a certain number of digits . Disable decimal_units if you do not want this enforced on units ( such as 15 . 000000 ) .
def shorten_number(n, limit=5, sig_figures=None, decimal_units=True):
    """Set a number over a certain length to something shorter.
    For example, 2000000 can be shortened to 2m.
    The numbers will be kept as long as possible, 
    so "2000k" will override "2m" at a length of 5.
    Set a minimum length to ensure it has a certain number of digits.
    Disable decimal_units if you do not want this enforced on units (such as 15.000000).
    """
    if sig_figures is None:
        sig_figures = limit - 1
    limits = [''] + list('kmbtq')
    i = 0
    str_n = str(int(n))
    max_length = max(limit, 4)
    
    try:
        #Reduce the number until it fits in the required space
        while True:
            prefix = limits[i]
            num_length = len(str_n)
            if num_length < max_length or not prefix and num_length <= max_length:
                break
            i += 1
            str_n = str_n[:-3]
        result = n / 10 ** (i*3)
        
        #Return whole number if decimal units are disabled
        if not decimal_units and not prefix:
            return str(int(result))
        
        #Convert to string if result is too large for a float
        overflow = 'e+' in str(result)
        if overflow:
            result = str(int(result)) + '.0'
            
        #Format the decimals based on required significant figures
        if overflow:
            int_length = len(result)
        else:
            int_length = len(str(int(result)))
        
        #Set maximum (and minimum) number of decimal points)
        max_decimals = max(0, sig_figures - int_length - bool(prefix))
        if sig_figures and max_decimals:
            result_parts = str(result).split('.')
            decimal = str(round(float('0.{}'.format(result_parts[1])), max_decimals))[2:]
            extra_zeroes = max_decimals - len(decimal)
            result = '{}.{}{}'.format(result_parts[0], decimal, '0' * extra_zeroes)
        else:
            if overflow:
                result = result.split('.')[0]
            else:
                result = int(result)
        return '{}{}'.format(result, prefix)
    
    #If the number goes out of limits, return that it's infinite
    except IndexError:
        return 'inf'
def shorten_number ( n , limit = 5 , sig_figures = None , decimal_units = True ) : if sig_figures is None : sig_figures = limit - 1 limits = [ '' ] + list ( 'kmbtq' ) i = 0 str_n = str ( int ( n ) ) max_length = max ( limit , 4 ) try : #Reduce the number until it fits in the required space while True : prefix = limits [ i ] num_length = len ( str_n ) if num_length < max_length or not prefix and num_length <= max_length : break i += 1 str_n = str_n [ : - 3 ] result = n / 10 ** ( i * 3 ) #Return whole number if decimal units are disabled if not decimal_units and not prefix : return str ( int ( result ) ) #Convert to string if result is too large for a float overflow = 'e+' in str ( result ) if overflow : result = str ( int ( result ) ) + '.0' #Format the decimals based on required significant figures if overflow : int_length = len ( result ) else : int_length = len ( str ( int ( result ) ) ) #Set maximum (and minimum) number of decimal points) max_decimals = max ( 0 , sig_figures - int_length - bool ( prefix ) ) if sig_figures and max_decimals : result_parts = str ( result ) . split ( '.' ) decimal = str ( round ( float ( '0.{}' . format ( result_parts [ 1 ] ) ) , max_decimals ) ) [ 2 : ] extra_zeroes = max_decimals - len ( decimal ) result = '{}.{}{}' . format ( result_parts [ 0 ] , decimal , '0' * extra_zeroes ) else : if overflow : result = result . split ( '.' ) [ 0 ] else : result = int ( result ) return '{}{}' . format ( result , prefix ) #If the number goes out of limits, return that it's infinite except IndexError : return 'inf'