-8

There is an issue with the Python round function when performing rounding operations. Why do different numbers have different rounding results? I feel confused about this function

enter image description here

I don't know why....

  • 3
    Post your code as text here, not a link to an image. – deceze Jul 18 '23 at 08:05
  • The code is as follows, Some floating-point numbers cannot be rounded correctly. >>> round(1.5) 2 >>> round(646.5) 646 >>> round(647.5) 648 – Delusion dream Jul 18 '23 at 08:09
  • 2
    [Edit] this into your question. But anyway, just look at the duplicate linked above. And define "correctly". If you claim something's "incorrect" or a bug, define what the correct behaviour should be and why. – deceze Jul 18 '23 at 08:09

1 Answers1

1

In Python, round will always round to the closest even number if it is equally spaced between two numbers.

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, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise, the return value has the same type as number.

https://docs.python.org/3/library/functions.html#round

Loocid
  • 6,112
  • 1
  • 24
  • 42