string formatting: engineering notation
Darren Dale
dd55 at cornell.edu
Wed Mar 14 18:03:22 EDT 2007
attn.steven.kuo at gmail.com wrote:
> 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)
>
That's a good suggestion. It's probably fast enough. I was hoping that
something like '%n'%my_number already existed.
More information about the Python-list
mailing list