473,386 Members | 1,693 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,386 software developers and data experts.

Output: Number of digits in exponent?

How can I control the number of digits in the exponent when writing
floats to a file? It seems that Python2.4.2(winXP) prints three digits anyway.
print 1.0e50 1e+050 print '%e' % (1.0e50) 1e+050

I would prefer two digits in the exponent. Usually 3 digits is not a
problem, except for waisted disk space in 99.999999% of all practical
cases.

But this time I have to write an interface file to a program written
in an other programming language, and the output format requires a fix
fortran format with 2 digits in the exponent.

Can this be done in Python? Speed is an issue so I don't like the
idea of rolling my own output function.

It should be possible to do: print '%12.5.2e' % (1.0e50)

1.000e+50

Thanks for any help!
Jens
Dec 6 '05 #1
2 3231
'%.4e' % 1.0e50

Dec 6 '05 #2
Jens Bloch Helmers <Je**********************@ATdnv.com> writes:
How can I control the number of digits in the exponent when writing
floats to a file? It seems that Python2.4.2(winXP) prints three
digits anyway.
print 1.0e50 1e+050
That's weird; must be version and/or OS dependent. On Fedora Core 4:

Python 2.4.1 (#1, May 16 2005, 15:19:29)
[GCC 4.0.0 20050512 (Red Hat 4.0.0-5)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
print 1e50
1e+50
Can this be done in Python? Speed is an issue so I don't like the
idea of rolling my own output function.

It should be possible to do: print '%12.5.2e' % (1.0e50)

1.000e+50


How's this (untested):

def efmt(x):
s = '%12.5e' % x
assert s[-5] == 'e' and s[-3] == '0'
return s[:-4] + s[-2:]
Dec 7 '05 #3

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

Similar topics

12
by: jose luis fernandez diaz | last post by:
Hi, My OS is: cronos:jdiaz:tmp>uname -a HP-UX cronos B.11.11 U 9000/800 820960681 unlimited-user license I compile in 64-bits mode the program below:
18
by: Bern | last post by:
how to specifiy a binary number in c++? hex numbers are specified by 0x prefix
13
by: CHRISTOF WARLICH | last post by:
Hi, does anyone know of an efficient way to find the number of digits (i.e. the most significant position that is 1) of a binary number? What I found so far is: - digits = (int) log2(number),...
37
by: Protoman | last post by:
Hi!!! Protoman here, I need to write an infinite precision number class b/c I want to compute pi. Give me some sample code. Also, when I run my program and it computes pi, will my computer freeze...
58
by: mailursubbu | last post by:
Hi How to write a program to get the factorial of 4096. I am working on a Linux m/c. Best Regards, Subra
1
by: Martin | last post by:
Hi. My question seems to be somewhat primitive, but I cannot find an appropriate solution. Is it possible to control the number of digits displayed in the exponent part of the scientific...
28
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
6
by: ocean | last post by:
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits. For example, it should output the individual digits...
3
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hello, By default, the "g" format specifier seems to use 2 digits in the exponent if it decides to use the scientific format. I.e., Double.ToString("g"). How do I control the number of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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: 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...

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.