5

I've seen a few questions about this already, but none that I read helped me actually understand why what I am trying to do is failing.

So I have a bunch of floating point values, and they have different precisions. Some are 0.1 others are 1.759374, etc. And I want to format them so they are ALL in the form of "+0.0000000E+00" I tried doing

number = '%1.7f' % oldnumber

but that didn't work. I thought what I was telling it to do was "one digit perfor the decimal point, and 7 after, float" but it doesn't work. I'm not really getting the examples in the docs, which don't seem to even bother with "before and after decimal point" issues, and I didn't find a question that was about before and after decimal point fixing.

Now, I know that some of my numbers are 0.0437 or similar, and I want them to appear as 4.3700000E-02 or something. I was sort of hoping it would do the E bit on it's own, but if it doesn't how do I do it?

Here is the exact line I have:

RealValConv =   '%1.7g' % struct.unpack('!f',    RealVal.decode('hex'))[0]

RealVal is a hex number that represents the value I want.

Also, this is in Python 2.7

3
  • You want to use g rather than f if you want scientific notation. For instance, "%1.7g" % .0000012323 becomes '1.2323e-06'. Is this the behavior you want? Commented Sep 10, 2013 at 0:30
  • Hmm, I'm not sure why, but that isn't working. I'm using python 2.7, does that matter? That is the behavior I wanted though. Commented Sep 10, 2013 at 0:39
  • @DavidRobinson: %g uses %f in some cases, e.g., formatting math.pi with %1.7g results in 3.141593. %e always uses "e" notation. Commented Sep 10, 2013 at 0:39

2 Answers 2

11
>>> '{:.7e}'.format(0.00000000000000365913456789)
'3.6591346e-15'
Sign up to request clarification or add additional context in comments.

2 Comments

My problems with length were an excel problem, not a program problem. (I open the file as .csv and excel defaulted it to being shorter)
Always, always, prefer .format over % formatting. Here is yet another reason it is preferred.
3

You can use the scientific notation format: Something like this:

number = '%e' % oldnumber

>>> x = 1.759374
>>> print '%e' % x
1.759374e+00
>>>
>>> x = 1.79
>>> print '%e' % x
1.790000e+00
>>>
>>> x = 1.798775655
>>> print '%e' % x
1.798776e+00
>>>

Or, if you want to control precision, you can use the format method as sugged by @leon approach (+1).

>>> x = 1.759374
>>>
>>> print('{:.2e}'.format(x))
1.76e+00
>>>
>>> print('{:.10e}'.format(x))
1.7593740000e+00
>>>
>>> print('{:.4e}'.format(x))
1.7594e+00
>>>

3 Comments

I actually get a default precision of 7 decimal values. If you want to control, then you can use format() method as suggested by @leon. I have updated my answer, accordingly.
My problems with length were an excel problem, not a program problem. (I open the file as .csv and excel defaulted it to being shorter) Sorry for assuming it was you, when it was me. Dizzy days.
Thnx for telling me that. Appreciate that!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.