Skip to content

Commit

Permalink
support.si_prefix: implement "real" to "scaled with si prefix" number…
Browse files Browse the repository at this point in the history
… conversion
  • Loading branch information
attie-argentum committed Oct 19, 2020
1 parent f9fbc92 commit 7c76880
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions software/glasgow/support/si_prefix.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
def num_to_si(num, long_prefix=False):
prefixes = [
( 3, 'G', 'Giga' ),
( 2, 'M', 'Mega' ),
( 1, 'k', 'Kilo' ),
( 0, '', '' ),
( -1, 'm', 'mili' ),
( -2, 'u', 'micro' ),
( -3, 'n', 'nano' ),
]
try:
factor, tshort, tlong = next(filter(lambda x: num > (1000 ** x[0]), prefixes))
except StopIteration:
factor, tshort, tlong = prefixes[-1]
prefix = tlong if long_prefix else tshort
return num * (1000 ** -factor), prefix

0 comments on commit 7c76880

Please sign in to comment.