Skip to content

Instantly share code, notes, and snippets.

@xaratustrah
Last active August 27, 2018 12:03
Show Gist options
  • Select an option

  • Save xaratustrah/b33404a343cd367c3cca393db2155d70 to your computer and use it in GitHub Desktop.

Select an option

Save xaratustrah/b33404a343cd367c3cca393db2155d70 to your computer and use it in GitHub Desktop.
get_number_notation.py
# show number notation
def get_number_notation(value, decimal_place=2, unit=''):
ref = {24: 'Y', 21: 'Z', 18: 'E', 15: 'P',
12: 'T', 9: 'G', 6: 'M', 3: 'k', 0: '',
-3: 'm', -6: 'u', -9: 'n', -12: 'p',
-15: 'f', -18: 'a', -21: 'z', -24: 'y',
}
num = max([key for key in ref.keys() if value > 10 ** key])
mult = ref[num] if unit else 'e{}'.format(num)
return '{}{}{}'.format(int(value / 10 ** num * 10 ** decimal_place) / 10 ** decimal_place, mult, unit)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment