Sorry, but I really don't know what's the meaning of the defination of round
in python 3.3.2 doc:
round(number[, ndigits])
Return the floating point value number rounded to ndigits digits after the decimal point. If ndigits is omitted, it defaults to zero. Delegates tonumber.__round__(ndigits)
.For the built-in types supporting
round()
, values are rounded to the closest multiple of 10 to the power minus ndigits if two multiples are equally close, rounding is done toward the even choice (so, for example, bothround(0.5)
andround(-0.5)
are0
, andround(1.5)
is2
). The return value is an integer if called with one argument, otherwise of the same type as number.
I don't know how come the multiple of 10 and pow
.
After reading the following examples, I think round(number,n)
works like:
if let number
be 123.456
, let n
be 2
round
will get two number:123.45
and123.46
round
comparesabs(number-123.45)
(0.006) andabs(number-123.46)
(0.004),and chooses the smaller one.so,
123.46
is the result.
and if let number
be 123.455
, let n
be 2
:
round
will get two number:123.45
and123.46
round
comparesabs(number-123.45)
(0.005) andabs(number-123.46)
(0.005). They are equal. Soround
checks the last digit of123.45
and123.46
. The even one is the result.so, the result is
123.46
Am I right?
If not, could you offer a understandable version of values are rounded to the closest multiple of 10 to the power minus ndigits?