[Tutor] Float and decimal spaces
Eric Walker
sli1que at yahoo.com
Sat Aug 4 15:43:49 CEST 2007
--- Kent Johnson <kent37 at tds.net> wrote:
> Toon Pieton wrote:
> > Hey all!
> >
> > My memory seem to have a horrible leak. How does
> one limit the amount of
> > decimal spaces for a float? I used to know that...
> >
> > Example: not 5.398042156, but 5.4 (if I decide to
> have only one place).
>
> For printing, use %.1f :
>
> In [2]: print '%.1f' % 5.398042156
> 5.4
>
> If you want to limit the precision of computations
> maybe the Decimal
> module would help.
>
> Kent
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
You can try this:
>>> test
5.3980421559999998
>>> print '%.2f' % (test)
5.40
>>>
The number gives the number of precision after the
decimal.
Eric
____________________________________________________________________________________
Moody friends. Drama queens. Your life? Nope! - their life, your story. Play Sims Stories at Yahoo! Games.
http://sims.yahoo.com/
More information about the Tutor
mailing list