Skip to content

Instantly share code, notes, and snippets.

@awade
Last active April 30, 2019 00:12
Show Gist options
  • Save awade/9b15a6ce4b6a6e173e27d5d52dd5ee97 to your computer and use it in GitHub Desktop.
Save awade/9b15a6ce4b6a6e173e27d5d52dd5ee97 to your computer and use it in GitHub Desktop.
Python function returns SI order of magnitude and string prefix. Requires numpy.
def convOOM(x):
'''Function to return tuple with nearest order of mag along with a
string prefix in the correct SI units.'''
assert isinstance(x, float), 'Function expect type float'
assert (1e-24 < x < 1e27), ('Range must be between 1.0e-24 and 1.0e26: '
'Exponent expression might be better choice than SI orders of mag.')
OOMlist = ['y', 'z', 'a', 'f', 'p', 'n', 'u', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
OOMnum = np.log10(x) // 3
return x * 10 ** (-3 * int(OOMnum)), OOMlist[int(OOMnum + 8)]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment