[Python] Why does Python 3 round half to even?

Discussão em 'Python' iniciado por Stack, Junho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I was just re-reading What’s New In Python 3.0 and it states:


    The round() function rounding strategy and return type have changed. Exact halfway cases are now rounded to the nearest even result instead of away from zero. (For example, round(2.5) now returns 2 rather than 3.)
    ​

    and the documentation for round():


    For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus n; if two multiples are equally close, rounding is done toward the even choice
    ​

    So, in Python 2 (for example, v2.7.3) I get the expected:

    round(2.5)
    3.0

    round(3.5)
    4.0


    However, now under Python 3 (for example v3.2.3):

    round(2.5)
    2

    round(3.5)
    4


    This seems counter-intuitive and contrary to what I understand about rounding (and bound to trip up people). English isn't my native language but until I read this I thought I knew what rounding meant :-/ I am sure at the time Python 3 was introduced there must have been some discussion of this, but I was unable to find a good reason in my search.

    1. Does anyone have insight into why this was changed to this?
    2. Are there any other mainstream programming languages (e.g., C, C++, Java, Perl, ..) that do this sort of (to me inconsistent) rounding?

    What am I missing here?

    UPDATE: @Li-aungYip's comment re "Banker's rounding" gave me the right search term/keywords to search for and I found this SO question: Why does .NET use banker's rounding as default?, so I will be reading that carefully.

    Continue reading...

Compartilhe esta Página