2012-05-18 11:03, Prof. Dr. Ulrich Tipp skrev:
If I write the following code
X = [0.1, 0.2]
html("Values: %s"%X)
the output is
Values: [10.1000000000000, 0.200000000000000]
How can I suppress the trailing zeros? How can I change the number of
digits shown?
The % operator between the " and the X is a python feature for formatted
printing. The "%s" is a format specifier asking for a conversion to
string and then printing.
The format specifier for a floating point number to be printed with 3
decimal places is %.3f, and the code would become:
html("Values: [%.3f, %.3f]"%(X[0], X[1]))
The problem making it awkward is that I convert both elements of the
list separately. For a general list, one could perhaps use
",".join(["%.3f" %x for x in X])
as in:
sage: X = [0.1, 0.2]
sage: "Values: [" + ",".join(["%.3f" % x for x in X]) + "]"
'Values: [0.100,0.200]'
sage:
The string formatting feature in python is documented at
<http://docs.python.org/library/stdtypes.html#string-formatting-operations>.
Regards
johan
--
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to
sage-support+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/sage-support
URL: http://www.sagemath.org