Skip to content

Instantly share code, notes, and snippets.

@eventuallyc0nsistent
Forked from thewarpaint/filter.js
Last active June 7, 2016 19:32
Show Gist options
  • Save eventuallyc0nsistent/1a69a7e265b642467e0338dc14cf34fb to your computer and use it in GitHub Desktop.
Save eventuallyc0nsistent/1a69a7e265b642467e0338dc14cf34fb to your computer and use it in GitHub Desktop.
Angular filter to convert numbers to thousand suffixes (1234 > 1.2k)
"""
Source: https://gist.github.com/thewarpaint/889690aeb21a8dfd7aba
"""
from __future__ import division
def number_suffixes(number):
suffixes = ['k', 'M', 'G', 'T', 'P', 'E']
if not isinstance(number, int):
return number
if number < 1000:
return number
exp = int(math.floor(math.log(number)/math.log(1000)))
result = '%.2f' % (number/math.pow(1000, exp)) + suffixes[exp - 1]
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment