Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
6-liner solution in Clear category for Friendly Number by przemyslaw.daniel
def friendly_number(number, base=1000, decimals=0, suffix='',
powers=['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']):
n = [number/base**x for x in range(len(powers))][::-1]
p = next((i for i, x in enumerate(n) if abs(x) >= 1), len(powers)-1)
r = ("%."+str(decimals)+"f") % (int(n[p]) if decimals == 0 else n[p])
return '%s%s%s' % (r, powers[len(powers)-p-1], suffix)
Oct. 8, 2016