[Tutor] Floats and rounding down
kent37 at tds.net
Sun Jan 7 06:57:29 CET 2007
Alan Gauld wrote:
> "Toon Pieton" <pytutmail at gmail.com> wrote
>> Something like temp = '$' + str(round(x,2)) will make x = $1.537
>> read as
>> $1.54. That's perfect. However, when x = 1.3, or x = 5.0, it will
>> just that: $1.3 or $5. I don't like that - I wan't 1.30 and 5.00,
>> that looks much more clean and homogeneous.
>> Is there any - not to complicated - way to do that?
> Yes, use string formatting.
>>>> print "$%0.2f" % 1.3
There is no need to round first, either:
In : print '$%0.2f' % 1.537
More information about the Tutor