Ask Your Question

Revision history [back]

Because Sage decimal numbers (elements of RR) do not have a proper format, I would recommend to transform them into a Python float before sending them to a string. In your case, you could use:

label_fmt = lambda x: r"$10^{{{0:.3g}}}$".format(float(log(x, 10)))

It yields

sage: contours=[1e-25,1e-24,1e-23,1e-22,1e-21,1e-20,1e-19]
sage: [label_fmt(c) for c in contours]
['$10^{-25}$',
 '$10^{-24}$',
 '$10^{-23}$',
 '$10^{-22}$',
 '$10^{-21}$',
 '$10^{-20}$',
 '$10^{-19}$']

NB: of course, 0 deserves a special treatment and has been excluded from the above list.