Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Friendly Number - improved solution in Clear category for Friendly Number by ddavidse
def friendly_number(number, base=1000, decimals=0, suffix='',
powers=['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']):
minus = '' if number >= 0 else '-'
number = abs(number)
n_reduced = number
power = ''
for i in range(len(powers)):
if number / base ** i >= 1:
n_reduced = number / base ** i
power = powers[i]
if decimals == 0:
number_out = str(int(n_reduced))
else:
s = str(n_reduced)
i = s.find('.')
if i != -1:
if len(s[i+1:]) >= decimals:
number_out = str(round(n_reduced, decimals))
else:
number_out = s + '0' * (decimals - len(s[i+1:]))
else:
number_out = s + '.' + '0' * decimals
return minus + number_out + power + suffix
Dec. 10, 2020