Connecting Tech Pros Worldwide Forums | Help | Site Map

string formatting: engineering notation

Darren Dale
Guest
 
Posts: n/a
#1: Mar 14 '07
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

Steve Holden
Guest
 
Posts: n/a
#2: Mar 14 '07

re: string formatting: engineering notation


Darren Dale wrote:
Quote:
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:
Quote:
Quote:
Quote:
>>"%.3e" % 3.14159
'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

Darren Dale
Guest
 
Posts: n/a
#3: Mar 14 '07

re: string formatting: engineering notation


Steve Holden wrote:
Quote:
Darren Dale wrote:
Quote:
>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:
>
Quote:
Quote:
>>"%.3e" % 3.14159
'3.142e+00'
Quote:
Quote:
Quote:
>>"%.3e" % 31415.9
'3.142e+04'

What I am looking for is '31.4159e+03'

Darren

Grant Edwards
Guest
 
Posts: n/a
#4: Mar 14 '07

re: string formatting: engineering notation


On 2007-03-14, Steve Holden <steve@holdenweb.comwrote:
Quote:
Darren Dale wrote:
Quote:
>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:
>
Quote:
Quote:
>>"%.3e" % 3.14159
'3.142e+00'
Not close at all. It should be "3.14159"
Quote:
Quote:
Quote:
>>"%.3e" % 31.4159
'3.142e+01'

should be 31.4159
Quote:
Quote:
Quote:
>>"%.3e" % 314.159
'3.142e+02'

should be 314.159
Quote:
Quote:
Quote:
>>"%.3e" % 31415.9
'3.142e+04'

should be 31.4159e3

--
Grant Edwards grante Yow! LOU GRANT froze
at my ASSETS!!
visi.com
Jorge Godoy
Guest
 
Posts: n/a
#5: Mar 14 '07

re: string formatting: engineering notation


Steve Holden <steve@holdenweb.comwrites:
Quote:
Darren Dale wrote:
Quote:
>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:
>
Quote:
Quote:
>>"%.3e" % 3.14159
'3.142e+00'
Quote:
Quote:
Quote:
>>"%.3e" % 314159
'3.142e+05'
Quote:
Quote:
Quote:
>>>
Not close when you have the exponent.



--
Jorge Godoy <jgodoy@gmail.com>
attn.steven.kuo@gmail.com
Guest
 
Posts: n/a
#6: Mar 14 '07

re: string formatting: engineering notation


On Mar 14, 1:14 pm, Darren Dale <d...@cornell.eduwrote:
Quote:
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

Darren Dale
Guest
 
Posts: n/a
#7: Mar 14 '07

re: string formatting: engineering notation


attn.steven.kuo@gmail.com wrote:
Quote:
On Mar 14, 1:14 pm, Darren Dale <d...@cornell.eduwrote:
Quote:
>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.
Closed Thread


Similar Python bytes