1

Under python 3.7.5 I execute the following code and get a weird output

for i in range(10):
    print(round(i+0.5))

enter image description here

If I understand correctly, any number that looks like xxx...x.5 with any digits x should always round up to xxx...x+1 under the round() operation, regardless whether the full integer part is even or odd. Am I making some mistake in my input? How to fix this? Thanks for any suggestion!

juanpa.arrivillaga
  • 88,713
  • 10
  • 131
  • 172
Kagaratsch
  • 963
  • 8
  • 18
  • If you change `0.5` to `0.51` you will get what you want. Refer [Floating point issues in Python](https://docs.python.org/3/tutorial/floatingpoint.html) – Sociopath Nov 13 '19 at 05:16
  • Possible duplicate of https://stackoverflow.com/q/10825926/6997665 – learner Nov 13 '19 at 05:18

1 Answers1

1

It's better if you use math module:
replace round with floor or ceil as you need:

from math import floor, ceil

now for round down use floor() and for round up use ceil()

print(floor(2.9))

output: 2

print(ceil(1.1))

output: 2