How to round a floating point to nearest 10?

Peter Otten __peter__ at web.de
Sat Aug 9 07:46:00 EDT 2008


Will Rocisky wrote:

> I want my 76.1 to be rounded to decimal 80 and 74.9 to decimal 70.
> How can I achieve that?

>>> help(round)

Help on built-in function round in module __builtin__:

round(...)
    round(number[, ndigits]) -> floating point number

    Round a number to a given precision in decimal digits (default 0 digits).
    This always returns a floating point number.  Precision may be negative.


>>> for f in 74.9, 75.0, 75.1:
...     print "%r --> %r" % (f, round(f, -1))
...
74.900000000000006 --> 70.0
75.0 --> 80.0
75.099999999999994 --> 80.0

Peter



More information about the Python-list mailing list