1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to properly round-up half float numbers?

Discussão em 'Python' iniciado por Stack, Setembro 7, 2025.

  1. Stack

    Stack Membro Participativo

    I am facing a strange behavior of the round() function:

    for i in range(1, 15, 2):
    n = i / 2
    print(n, "=>", round(n))


    This code prints:

    0.5 => 0
    1.5 => 2
    2.5 => 2
    3.5 => 4
    4.5 => 4
    5.5 => 6
    6.5 => 6


    I expected the floating values to be always rounded up, but instead, it is rounded to the nearest even number.

    Why such behavior, and what is the best way to get the correct result?

    I tried to use the fractions but the result is the same.

    Continue reading...

Compartilhe esta Página