string formatting: engineering notation

D

Darren Dale

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?

Thanks,
Darren
 
S

Steve Holden

Darren said:
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?
How close is this:
'3.142e+00'

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Blog of Note: http://holdenweb.blogspot.com
See you at PyCon? http://us.pycon.org/TX2007
 
G

Grant Edwards

How close is this:

'3.142e+00'

Not close at all. It should be "3.14159"
'3.142e+01'

should be 31.4159
'3.142e+02'

should be 314.159
'3.142e+04'

should be 31.4159e3
 
A

attn.steven.kuo

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)
 
D

Darren Dale

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top