Convert float number to string with engineering notation (with SI prefix) in Python
Here is a function inspired from Formatting a number with a metric prefix?
metric.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import math
def to_si(d, sep=' '):
"""
Convert number to string with SI prefix
:Example:
>>> to_si(2500.0)
'2.5 k'
>>> to_si(2.3E6)
'2.3 M'
>>> to_si(2.3E-6)
'2.3 µ'
>>> to_si(-2500.0)
'-2.5 k'
>>> to_si(0)
'0'
"""
inc_prefixes = ['k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y']
dec_prefixes = ['m', 'µ', 'n', 'p', 'f', 'a', 'z', 'y']
if d == 0:
return str(0)
degree = int(math.floor(math.log10(math.fabs(d)) / 3))
prefix = ''
if degree != 0:
ds = degree / math.fabs(degree)
if ds == 1:
if degree - 1 < len(inc_prefixes):
prefix = inc_prefixes[degree - 1]
else:
prefix = inc_prefixes[-1]
degree = len(inc_prefixes)
elif ds == -1:
if -degree - 1 < len(dec_prefixes):
prefix = dec_prefixes[-degree - 1]
else:
prefix = dec_prefixes[-1]
degree = -len(dec_prefixes)
scaled = float(d * math.pow(1000, -degree))
s = "{scaled}{sep}{prefix}".format(scaled=scaled,
sep=sep,
prefix=prefix)
else:
s = "{d}".format(d=d)
return s
if __name__ == "__main__":
import doctest
doctest.testmod()
and its usage:
from metric import to_si
d = 23392342.1
print(to_si(d))
It will display
23.3923421 M
Use the QuantiPhy package. It is a stable well documented and well supported package that is designed to do just what you want.
>>> from quantiphy import Quantity
>>> v = Quantity(23.3923421E6)
>>> str(v)
'23.392M'
>>> v.render(prec='full')
'23.3923421M'
Generally people use SI unit prefixes with units, and Quantity is designed to combine the units with the number.
>>> v = Quantity(23.3923421E6, 'V')
>>> print(v)
23.392 MV
>>> f = Quantity('23.3923421 MHz')
>>> print('{}'.format(f))
23.392 MHz
Quantity subclasses float, so you can use quantities in expressions as you would a float:
>>> t = 1/f
>>> print(t)
4.274903281275114e-08
>>> t = Quantity(t, 's')
>>> print(t)
42.749 ns
A direct solution is to use the Decimal.to_eng_string method and then do a dictionary lookup to convert the exponent to appropriate metric prefix.