[Tutor] Scientific Notation + 18 digit precision

Alan Gauld alan.gauld at freenet.co.uk
Sun Nov 27 10:13:23 CET 2005


> Is there an easy  way to convert these large numbers to the scientific 
> notation format?

The numbers are, of course, stored in binary so the real question is:
can we *display* them in scientific format?

The answer is yes using string format operators.

Try playing with the %e and %g options:

>>> val = 123456789453286432976.5309764
>>> print "%e" % val
>>> print "%g" % val
>>> print "%E" % val
>>> print "%G" % val

You can also control how many decimal places are displayed too

>>> print "%.4" % val

And the total number of digits, and the justification. Well worth
browsing the documentation on string formatting.

There are also ways of getting engineering notation (ie the powers
are multiples of 3, but I've forgotten for now how that's done!)

HTH

Alan G
Author of the learn to program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld




More information about the Tutor mailing list