1

i want to display numbers in scientific notation:

-407.45833

So far i used this:

        i = '%E' % Decimal(i)

result:

-4.074583E+02

now my question: how can i add one more digit so it looks like this:?

-4.074583E+002 

I know i should find my answer in the tables from Python string formatting to select the proper format layout, but i cant find it. Can someone tell me the result and where to find it please?

Vipr0
  • 59
  • 7

1 Answers1

1

There's unfortunately not any direct way using string formatting to have three digits following the +. A easy method to replace it is to use this since what we know is that is exponential forms are all stored as strings, so all the string methods will work on it.

I wrote a little function that takes a regular scientific notation and returns a formatted notation with three digits after the +:

from decimal import Decimal
def pretty(notation, n):
    if '+' in notation:
        return "+".join([notation.split('+')[0],notation.split('+')[1].zfill(n)])
    return "-".join([notation.split('-')[0],notation.split('-')[1].zfill(n)])

i = '%E' % Decimal(-407.45833)
print(pretty(i,3)) # leave three digits
Taku
  • 31,927
  • 11
  • 74
  • 85
  • That's great, if it actually helped you, you can mark my answer as accepted by checking the ✅. And have a great day:) – Taku Mar 25 '17 at 19:22