string formatting: engineering notation
attn.steven.kuo at gmail.com
attn.steven.kuo at gmail.com
Wed Mar 14 17:48:35 EDT 2007
On Mar 14, 1:14 pm, Darren Dale <d... at cornell.edu> wrote:
> Does anyone know if it is possible to represent a number as a string with
> engineering notation (like scientific notation, but with 10 raised to
> multiples of 3: 120e3, 12e-6, etc.). I know this is possible with the
> decimal.Decimal class, but repeatedly instantiating Decimals is inefficient
> for my application (matplotlib plotting library). If it is not currently
> possible, do you think the python devs would be receptive to including
> support for engineering notation in future releases?
Do you also consider this to be too inefficient?
import math
for exponent in xrange(-10, 11):
flt = 1.23 * math.pow(10, exponent)
l = math.log10(flt)
if l < 0:
l = l - 3
p3 = int(l / 3) * 3
multiplier = flt / pow(10, p3)
print '%e => %fe%d' % (flt, multiplier, p3)
--
Hope this helps,
Steven
More information about the Python-list
mailing list