[Tutor] Rounding off

Dick Moores rdm at rcblue.com
Sun Mar 18 14:39:59 CET 2007


At 04:59 AM 3/18/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


 >>> print "%.4g" % 2.1345
2.135

But now I have a question.

 >>> print "%.3f" % 22.1345
22.134

and

 >>> print "%.5g" % 22.1345
22.134

but

 >>> print "%.3f" % 22.1346
22.135

and

 >>> print "%.5g" % 22.1346
22.135

Why the 22.134's?

Dick Moores 



More information about the Tutor mailing list