Skip to content

Instantly share code, notes, and snippets.

@Reflejo
Created September 26, 2016 23:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Reflejo/bad89882699b11bb2208c6d9275c19ab to your computer and use it in GitHub Desktop.
Save Reflejo/bad89882699b11bb2208c6d9275c19ab to your computer and use it in GitHub Desktop.
import bisect
from operator import itemgetter
units = [
(1e-24, 'y'),
(1e-21, 'z'),
(1e-18, 'a'),
(1e-15, 'f'),
(1e-12, 'p'),
(1e-9, 'n'),
(1e-6, 'u'),
(1e-3, 'm'),
(1e-2, 'c'),
(1e-1, 'd'),
(1e3, 'k'),
(1e6, 'M'),
(1e9, 'G'),
(1e12, 'T'),
(1e15, 'P'),
(1e18, 'E'),
(1e21, 'Z'),
(1e24, 'Y'),
]
idx = bisect.bisect(map(itemgetter(0), units), 42e6) - 1
magnitude, postfix = units[idx]
print 42e6 / magnitude, postfix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment