-1

I was playing around with the Python round function when I noticed something weird. See for yourself:

CMD output:

1.5
2.5
0.5
2
2
0

Code:

a = 1.5 
b = 2.5
c = 0.5
print(a)
print(b)
print(c)
print(round(a))
print(round(b))
print(round(c))

What's going on?

  • 3
    Only Mark Dickinson really understands how `round()` works ;) https://stackoverflow.com/a/22155830/6260170 – Chris_Rands Feb 13 '18 at 14:44
  • 1
    "if two multiples are equally close, rounding is done toward the even choice" — https://docs.python.org/3/library/functions.html#round – khelwood Feb 13 '18 at 14:48
  • 1
    Okay, so what *is* the weird thing you notice? Perhaps it's something else than we all seem to be assuming. (Use [edit] to add it into your question.) – Jongware Feb 13 '18 at 15:19
  • though technically the _question_ is not a duplicate persay... the answer from Mark Dickinson does answer this question when giving word of caution in the other question – Newtopian Feb 13 '18 at 15:29
  • This is thoroughly addressed in the second answer on the duplicate question - https://stackoverflow.com/a/22155830/146077 – Kirk Broadhurst Feb 13 '18 at 16:31

1 Answers1

-5
a = 1.5 
b = 2.5
c = 0.5
print(a)
print(b)
print(c)
print(round(a,0))
print(round(b,0))
print(round(c,0))

You can try it