473,396 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

string formatting: engineering notation

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
Mar 14 '07 #1
6 6643
Darren Dale 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?
How close is this:
>>"%.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

Mar 14 '07 #2
Steve Holden wrote:
Darren Dale 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?
How close is this:
>>"%.3e" % 3.14159
'3.142e+00'
>>"%.3e" % 31415.9
'3.142e+04'

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

Darren

Mar 14 '07 #3
On 2007-03-14, Steve Holden <st***@holdenweb.comwrote:
Darren Dale 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?
How close is this:
>>"%.3e" % 3.14159
'3.142e+00'
Not close at all. It should be "3.14159"
>>"%.3e" % 31.4159
'3.142e+01'

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

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

should be 31.4159e3

--
Grant Edwards grante Yow! LOU GRANT froze
at my ASSETS!!
visi.com
Mar 14 '07 #4
Steve Holden <st***@holdenweb.comwrites:
Darren Dale 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?
How close is this:
>>"%.3e" % 3.14159
'3.142e+00'
>>"%.3e" % 314159
'3.142e+05'
>>>
Not close when you have the exponent.

--
Jorge Godoy <jg****@gmail.com>
Mar 14 '07 #5
On Mar 14, 1:14 pm, Darren Dale <d...@cornell.eduwrote:
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

Mar 14 '07 #6
at*************@gmail.com wrote:
On Mar 14, 1:14 pm, Darren Dale <d...@cornell.eduwrote:
>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.
Mar 14 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: LRW | last post by:
OK, this is probably an odd, but esy question. =) I taught myself PHP from a Julie Meloni book and newsgroup questions. So, I don't think I've really learned the proper way of organizing and...
3
by: David Sharp | last post by:
I'm trying to find a way to format a FLOAT variable into a varchar in SQL Server 2000 but using CAST/CONVERT I can only get scientific notation i.e. 1e+006 instead of 1000000 which isn't really...
102
by: Steve Johnson | last post by:
I'd like to hear thoughts on what books, in your opinion, are true classics in the field of software engineering. I read a lot on the topic - at least a book a month for many years. There are many...
2
by: CJ | last post by:
This is a really silly question, but I just can't get it right. I have a double; the value is 0.00003025. I want to display it exactly as it stands, but by default it is displayed in scientific...
16
by: John Salerno | last post by:
My initial feeling is that concatenation might take longer than substitution, but that it is also easier to read: def p(self, paragraph): self.source += '<p>' + paragraph + '</p>\n\n' vs. ...
7
by: OriginalBrownster | last post by:
Hi there: I was wondering if its at all possible to search through a string for a specific character. I want to search through a string backwords and find the last period/comma, then take...
6
by: Scewbedew | last post by:
Suppose I have the following code: string myFormat = "Line1/nLine 2"; string formattedString = string.Format(myFormat); ....that would produce a 2-line output as expected. But if I load...
2
by: mary | last post by:
Hi, for my thesis at the university I'm working on a Visual c++ 6.0 source code, to understand better it I need to extract the UML graphics: Class Diagram, Object Diagram, Use Case Diagram, State...
19
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I access a property of an object using a string?...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.