-1

What is wrong with the round function in Python 3.6.3? I tested the code below:

a = 0.5
b = 0.5000001
print (round(a),round(b))

..and get result as

0 1

So rounding 0.5 -> 0 but 0.500001 -> 1. Should both variables get value of 1?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
  • 1
    I would expect the output you received... See [bankers rounding](http://wiki.c2.com/?BankersRounding) – Leonardo Alves Machado Dec 29 '17 at 11:46
  • 6
    There's nothing wrong with it. Per [the docs](https://docs.python.org/3/library/functions.html#round) Python 3.x rounds half to even by default. – jonrsharpe Dec 29 '17 at 11:46
  • Yes they should and it does in 2.7 and 3 as well, idk why it's not working right quiet strange , but whatever , i have seen stranger things! – Ubdus Samad Dec 29 '17 at 11:47
  • @UbdusSamad: you may want to read the answer to the duplicate (and links therein) for the reasoning behind this. – Jongware Dec 29 '17 at 11:57

2 Answers2

1

It's described in docs:

If ndigits is omitted or is None, it returns the nearest integer to its input.

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).

gonczor
  • 3,994
  • 1
  • 21
  • 46
-1

Both variable will not get value 1. because the value between range from 0.0000000 to 0.5000000 will round off to 0 ,but when you increase any decimal bit after 0.5 it will round off to 1.

Daneshkhan
  • 41
  • 4