Formating complex numbers

Huaiyu Zhu hzhu at mars.localdomain
Tue Jun 13 03:47:21 EDT 2000


Is there a way to format complex numbers like

x = (1 + 2j)/3.
print "%8.3g" % x

instead of using the following?

def format(x): return "%8s" % ("% .3g% +.3gj" % (x.real, x.imag))
print format(x)

More generally, can % be overloaded?  I'd like to see the syntax changed so
that %8.3G or %.4F (with upper case symbols) allow optional complex
arguments. The format %s does not allow one to prescribe the precisions.  At
the moment the upper case %G %E are used to given uppercase E in the output,
which is sort of pointless.

I can hear people say, "but the C lib ...".  Well, the C lib doesn't like
("%s" % 3) or ("%s" % math.exp) either but python still got it.


Huaiyu



More information about the Python-list mailing list