[Tutor] Help on float & round.

Alan Gauld alan.gauld@blueyonder.co.uk
Wed Jun 11 02:49:01 2003


> What I want is for it to print at 70.70, but not be a string, but a
float for
> math purposes.

The key here is that you only want to round for printing. So the best
solution is a format string:

print "%5.2f" % stock[2]

will print the number as at least 5 characters, two of which are after
the decimal point.

Thus

7.070006  -> ' 7.07'     # pads to 5 chars, rounding down
1237.89   -> '1237.89'   # prints *at least* 5 chars
12.56921  -> '12.57'     # rounds up

Look at the first page of the Basics section ofmy tutor for more
info on format strings.

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