1

I borrowed this little scientific notation script from another poster: Display a decimal in scientific notation.

def format_e(n):
    a = '%E' % n
    return a.split('E')[0].rstrip('0').rstrip('.') + 'E' + a.split('E')[1]

format_e(Decimal('40800000000.00000000000000'))
# '4.08E+10'

format_e(Decimal('40000000000.00000000000000'))
# '4E+10'

format_e(Decimal('40812300000.00000000000000'))

The function works fine when manipulated as module from the Terminal or the Python Shell. However, when ran like 'python Converter.py' it terminates immediately without returning any of the three examples above.

Community
  • 1
  • 1
WorkShoft
  • 440
  • 6
  • 18
  • Note the problem description in the updated title.. so, what is special about running interactively? – user2864740 Sep 10 '14 at 21:01
  • Probably nothing, I use gedit then run from the command line 99% of the time. Sleep deprivation has some special effects, though. – WorkShoft Sep 10 '14 at 21:17

1 Answers1

4

The interactive interpreter auto-echoes the results of any expression that doesn't return None. In a script you need to explicitly print the results you want to see:

print format_e(Decimal('40800000000.00000000000000'))
print format_e(Decimal('40000000000.00000000000000'))
print format_e(Decimal('40812300000.00000000000000'))
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343