[Tutor] Rounding off
Kent Johnson
kent37 at tds.net
Sun Mar 18 13:55:21 CET 2007
Dick Moores wrote:
> At 03:08 AM 3/18/2007, Per Jr. Greisen wrote:
>> Hi,
>>
>> Is there an easy way - a module or method for which you can round
>> off number such as
>>
>> 2.1345 to 2.135
>
> Well, there are
>
> >>> print "%.3f" % 2.1345
> 2.135
>
> or
>
> >>> print round(2.1345, 3)
> 2.135
The difference between these two is not obvious from the output. The
result of "%.3f" % 2.1345 is a string; the result of round() is a
floating-point number. So if your objective is display, use "%.3f" %
2.1345. If your objective is further calculations, use round().
Kent
More information about the Tutor
mailing list