# Rounding a number to nearest even

Discussion in 'Python' started by bdsatish, Apr 11, 2008.

1. ### bdsatishGuest

The built-in function round( ) will always "round up", that is 1.5 is
rounded to 2.0 and 2.5 is rounded to 3.0.

If I want to round to the nearest even, that is

my_round(1.5) = 2 # As expected
my_round(2.5) = 2 # Not 3, which is an odd num

I'm interested in rounding numbers of the form "x.5" depending upon
whether x is odd or even. Any idea about how to implement it ?

bdsatish, Apr 11, 2008

2. ### Guest

On 11 avr, 12:14, bdsatish <> wrote:
> The built-in function round( ) will always "round up", that is 1.5 is
> rounded to 2.0 and 2.5 is rounded to 3.0.
>
> If I want to round to the nearest even, that is
>
> my_round(1.5) = 2 # As expected
> my_round(2.5) = 2 # Not 3, which is an odd num
>
> I'm interested in rounding numbers of the form "x.5" depending upon
> whether x is odd or even. Any idea about how to implement it ?

When you say "round to the nearest even", you mean new_round(3) <> 3?

Is so, you can try:

In [37]: def new_round(x):
....: return round(x/2.)*2
....:

In [38]: new_round(1.5)
Out[38]: 2.0

In [39]: new_round(2.5)
Out[39]: 2.0

In [40]: new_round(3)
Out[40]: 4.0

, Apr 11, 2008

3. ### bdsatishGuest

On Apr 11, 3:27 pm, wrote:
> On 11 avr, 12:14, bdsatish <> wrote:
>
> > The built-in function round( ) will always "round up", that is 1.5 is
> > rounded to 2.0 and 2.5 is rounded to 3.0.

>
> > If I want to round to the nearest even, that is

>
> > my_round(1.5) = 2 # As expected
> > my_round(2.5) = 2 # Not 3, which is an odd num

>
> > I'm interested in rounding numbers of the form "x.5" depending upon
> > whether x is odd or even. Any idea about how to implement it ?

>
> When you say "round to the nearest even", you mean new_round(3) <> 3?

No. not at all. The clause "nearest even" comes into picture only when
a number is of form "x.5" or else it's same as builtin round( ).
new_round(3.0) must be 3.0 itself. Here is the mathematical definition
of what I want:

If 'n' is an integer,

new_round(n+0.5) = n if n/2 is integer
new_round(n+0.5) = (n+1) if (n+1)/2 is integer

In all other cases, new_round() behave similarly as round( ). Here are
the results I expect:

new_round(3.2) = 3
new_round(3.6) = 4
new_round(3.5) = 4
new_round(2.5) = 2
new_round(-0.5) = 0.0
new_round(-1.5) = -2.0
new_round(-1.3) = -1.0
new_round(-1.8) = -2
new_round(-2.5) = -2.0

The built-in function doesnt meet my needs for round(-2.5) or
round(2.5)

bdsatish, Apr 11, 2008
4. ### Guest

couldn't you just do.

#untested
new_round(n):
else:

, Apr 11, 2008
5. ### Guest

On Apr 11, 1:19 pm, wrote:
> couldn't you just do.
>
> #untested
> new_round(n):
> # is answer now odd
> else:

Whoops, this also affects odd numbers...

Will try and find a GOOD solution later...

Strange request though, why do you need it that way, because 2.5 is
CLOSER to 3 than to 2...

, Apr 11, 2008
6. ### bdsatishGuest

On Apr 11, 4:19 pm, wrote:
> couldn't you just do.
>
> #untested
> new_round(n):
> # is answer now odd
> else:

It fails for negative numbers: For -2.5 it gives -4.0 as answer
whereas I expect -2.0

bdsatish, Apr 11, 2008
7. ### bdsatishGuest

On Apr 11, 4:24 pm, wrote:
> On Apr 11, 1:19 pm, wrote:
>
> > couldn't you just do.

>
> > #untested
> > new_round(n):
> > # is answer now odd
> > if answer % 2:
> > return answer - 1
> > else:

>
> Whoops, this also affects odd numbers...
>
> Will try and find a GOOD solution later...
>
> Strange request though, why do you need it that way, because 2.5 is
> CLOSER to 3 than to 2...

It also fails for negative numbers. For -2.5 as input, I get -4.0
whereas I expect -2.0

This is a lengthy solution I came-up with:

def round_even(x):
temp = round(abs(x))
if (abs(x) - 0.5)%2.0 == 0.0: temp=temp-1
return signum(x)*temp

def signum(x):
if x>0: return 1
if x<0: return -1
return 0

But i guess there are better ways. I need it 'cos I'm translating some
code from Mathematica to Python. And Math..ica's Round[ ] behaves this
way (as I requested)

bdsatish, Apr 11, 2008
8. ### bdsatishGuest

On Apr 11, 4:37 pm, Scott David Daniels <> wrote:
> bdsatish wrote:
> > The built-in function round( ) will always "round up", that is 1.5 is

> def rounded(v):
> rounded = round(v)
> if divmod(v, 1)[1] == .5 and divmod(rounded, 2)[1] == 1:
> if v > 0:
> return rounded - 1
> return rounded + 1
> return rounded
>
> last = None
> for n in range(-29, 28):
> x = n * .25
> r = xr(x)
> if r != last:
> last = r
> print
> print '%s->%s' % (x, xr(x)),
>

Hi Scott,
This is what I was looking for.. I forgot about divmod( ) thanks for
reminding.

bdsatish, Apr 11, 2008
9. ### Gerard FlanaganGuest

On Apr 11, 12:14 pm, bdsatish <> wrote:
> The built-in function round( ) will always "round up", that is 1.5 is
> rounded to 2.0 and 2.5 is rounded to 3.0.
>
> If I want to round to the nearest even, that is
>
> my_round(1.5) = 2 # As expected
> my_round(2.5) = 2 # Not 3, which is an odd num
>
> I'm interested in rounding numbers of the form "x.5" depending upon
> whether x is odd or even. Any idea about how to implement it ?

------------------------------------------------
def myround(x):
n = int(x)
if abs(x - n) == 0.5:
if n % 2:
#it's odd
return n + 1 - 2 * int(n<0)
else:
return n
else:
return round(x)

assert myround(3.2) == 3
assert myround(3.6) == 4
assert myround(3.5) == 4
assert myround(2.5) == 2
assert myround(-0.5) == 0.0
assert myround(-1.5) == -2.0
assert myround(-1.3) == -1.0
assert myround(-1.8) == -2
assert myround(-2.5) == -2.0
------------------------------------------------

Gerard Flanagan, Apr 11, 2008
10. ### Gerard FlanaganGuest

On Apr 11, 2:05 pm, Gerard Flanagan <> wrote:
> On Apr 11, 12:14 pm, bdsatish <> wrote:
>
> > The built-in function round( ) will always "round up", that is 1.5 is
> > rounded to 2.0 and 2.5 is rounded to 3.0.

>
> > If I want to round to the nearest even, that is

>
> > my_round(1.5) = 2 # As expected
> > my_round(2.5) = 2 # Not 3, which is an odd num

>
> > I'm interested in rounding numbers of the form "x.5" depending upon
> > whether x is odd or even. Any idea about how to implement it ?

>
> ------------------------------------------------
> def myround(x):
> n = int(x)
> if abs(x - n) == 0.5:
> if n % 2:
> #it's odd
> return n + 1 - 2 * int(n<0)
> else:
> return n
> else:
> return round(x)
>
> ------------------------------------------------

In fact you can avoid the call to the builtin round:

------------------------------------------------
def myround(x):
n = int(x)
if abs(x - n) >= 0.5 and n % 2:
return n + 1 - 2 * int(n<0)
else:
return n

assert myround(3.2) == 3
assert myround(3.6) == 4
assert myround(3.5) == 4
assert myround(2.5) == 2
assert myround(-0.5) == 0.0
assert myround(-1.5) == -2.0
assert myround(-1.3) == -1.0
assert myround(-1.8) == -2
assert myround(-2.5) == -2.0
------------------------------------------------

Gerard Flanagan, Apr 11, 2008
11. ### bdsatishGuest

HI Gerard,

I think you've taken it to the best possible implementation. Thanks !
On Apr 11, 5:14 pm, Gerard Flanagan <> wrote:
> In fact you can avoid the call to the builtin round:
>
> ------------------------------------------------
> def myround(x):
> n = int(x)
> if abs(x - n) >= 0.5 and n % 2:
> return n + 1 - 2 * int(n<0)
> else:
> return n
>
> assert myround(3.2) == 3
> assert myround(3.6) == 4
> assert myround(3.5) == 4
> assert myround(2.5) == 2
> assert myround(-0.5) == 0.0
> assert myround(-1.5) == -2.0
> assert myround(-1.3) == -1.0
> assert myround(-1.8) == -2
> assert myround(-2.5) == -2.0
> ------------------------------------------------

bdsatish, Apr 11, 2008
12. ### Guest

On 11 avr, 14:14, Gerard Flanagan <> wrote:
> On Apr 11, 2:05 pm, Gerard Flanagan <> wrote:
>
> > On Apr 11, 12:14 pm, bdsatish <> wrote:

>
> > > The built-in function round( ) will always "round up", that is 1.5 is
> > > rounded to 2.0 and 2.5 is rounded to 3.0.

>
> > > If I want to round to the nearest even, that is

>
> > > my_round(1.5) = 2 # As expected
> > > my_round(2.5) = 2 # Not 3, which is an odd num

>
> > > I'm interested in rounding numbers of the form "x.5" depending upon
> > > whether x is odd or even. Any idea about how to implement it ?

> In fact you can avoid the call to the builtin round:

Alternatively, you can avoid the test using both divmod and round:

In [55]: def myround(x):
.....: d, m = divmod(x, 2)
.....: return 2*d + 1 + round(m-1)
.....:

In [58]: assert myround(3.2) == 3

In [59]: assert myround(3.6) == 4

In [60]: assert myround(3.5) == 4

In [61]: assert myround(2.5) == 2

In [62]: assert myround(-0.5) == 0.0

In [63]: assert myround(-1.5) == -2.0

In [64]: assert myround(-1.3) == -1.0

In [65]: assert myround(-1.8) == -2

In [66]: assert myround(-2.5) == -2.0

, Apr 11, 2008
13. ### bdsatishGuest

On Apr 11, 5:33 pm, bdsatish <> wrote:
> HI Gerard,
>
> I think you've taken it to the best possible implementation. Thanks !
> On Apr 11, 5:14 pm, Gerard Flanagan <> wrote:
>
> > In fact you can avoid the call to the builtin round:

>
> > ------------------------------------------------

>
> > assert myround(3.2) == 3
> > assert myround(3.6) == 4
> > assert myround(3.5) == 4
> > assert myround(2.5) == 2
> > assert myround(-0.5) == 0.0
> > assert myround(-1.5) == -2.0
> > assert myround(-1.3) == -1.0
> > assert myround(-1.8) == -2
> > assert myround(-2.5) == -2.0
> > ------------------------------------------------

OK, I was too early to praise Gerard. The following version:

def myround(x):
n = int(x)
if abs(x - n) >= 0.5 and n % 2:
return n + 1 - 2 * int(n<0)
else:
return n

of Gerard doesn't work for 0.6 (or 0.7, etc.) It gives the answer 0
but I would expect 1.0 ( because 0.6 doesn't end in 0.5 at all... so
usual rules of round( ) apply)

bdsatish, Apr 11, 2008
14. ### Ivan IllarionovGuest

On Apr 11, 2:14 pm, bdsatish <> wrote:
> The built-in function round( ) will always "round up", that is 1.5 is
> rounded to 2.0 and 2.5 is rounded to 3.0.
>
> If I want to round to the nearest even, that is
>
> my_round(1.5) = 2 # As expected
> my_round(2.5) = 2 # Not 3, which is an odd num
>
> I'm interested in rounding numbers of the form "x.5" depending upon
> whether x is odd or even. Any idea about how to implement it ?

def even_round(x):
if x % 1 == .5 and not (int(x) % 2):
return float(int(x))
else:
return round(x)

nums = [ 3.2, 3.6, 3.5, 2.5, -.5, -1.5, -1.3, -1.8, -2.5 ]
for num in nums:
print num, '->', even_round(num)

3.2 -> 3.0
3.6 -> 4.0
3.5 -> 4.0
2.5 -> 2.0
-0.5 -> 0.0
-1.5 -> -2.0
-1.3 -> -1.0
-1.8 -> -2.0
-2.5 -> -2.0

Ivan Illarionov, Apr 11, 2008
15. ### hdanteGuest

On Apr 11, 9:45 am, bdsatish <> wrote:
> On Apr 11, 5:33 pm, bdsatish <> wrote:
>
>
>
> > HI Gerard,

>
> > I think you've taken it to the best possible implementation. Thanks !
> > On Apr 11, 5:14 pm, Gerard Flanagan <> wrote:

>
> > > In fact you can avoid the call to the builtin round:

>
> > > ------------------------------------------------

>
> > > assert myround(3.2) == 3
> > > assert myround(3.6) == 4
> > > assert myround(3.5) == 4
> > > assert myround(2.5) == 2
> > > assert myround(-0.5) == 0.0
> > > assert myround(-1.5) == -2.0
> > > assert myround(-1.3) == -1.0
> > > assert myround(-1.8) == -2
> > > assert myround(-2.5) == -2.0
> > > ------------------------------------------------

>
> OK, I was too early to praise Gerard. The following version:
>
> def myround(x):
> n = int(x)
> if abs(x - n) >= 0.5 and n % 2:
> return n + 1 - 2 * int(n<0)
> else:
> return n
>
> of Gerard doesn't work for 0.6 (or 0.7, etc.) It gives the answer 0
> but I would expect 1.0 ( because 0.6 doesn't end in 0.5 at all... so
> usual rules of round( ) apply)

Interestingly, you could solve this by using python 3.
round(x[, n])
Return the floating point value x rounded to n digits after the
decimal point. If n is omitted, it defaults to zero. 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, for example, both round(0.5) and round(-0.5) are 0, and
round(1.5) is 2). Delegates to x.__round__(n).

My turn: ;-)

def yaround(x):
i = int(x)
f = x - i
if f != 0.5 and f != -0.5: return round(x)
return 2.0*round(x/2.0)

a = (-10.0, -9.0, -8.0, -4.6, -4.5, -4.4, -4.0, -3.6, -3.5,
-3.4, -0.6, -0.5, -0.4, 0.0, 0.4, 0.5, 0.6, 0.9, 1.0,
1.4, 1.5, 1.6, 2.0, 2.4, 2.5, 2.6, 10.0, 100.0)

b = (-10.0, -9.0, -8.0, -5.0, -4.0, -4.0, -4.0, -4.0, -4.0,
-3.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 10.0, 100.0)

for i in range(len(a)):
assert yaround(a) == b

hdante, Apr 11, 2008
16. ### Ivan IllarionovGuest

On Apr 11, 5:49 pm, hdante <> wrote:
> On Apr 11, 9:45 am, bdsatish <> wrote:
>
>
>
> > On Apr 11, 5:33 pm, bdsatish <> wrote:

>
> > > HI Gerard,

>
> > > I think you've taken it to the best possible implementation. Thanks !
> > > On Apr 11, 5:14 pm, Gerard Flanagan <> wrote:

>
> > > > In fact you can avoid the call to the builtin round:

>
> > > > ------------------------------------------------

>
> > > > assert myround(3.2) == 3
> > > > assert myround(3.6) == 4
> > > > assert myround(3.5) == 4
> > > > assert myround(2.5) == 2
> > > > assert myround(-0.5) == 0.0
> > > > assert myround(-1.5) == -2.0
> > > > assert myround(-1.3) == -1.0
> > > > assert myround(-1.8) == -2
> > > > assert myround(-2.5) == -2.0
> > > > ------------------------------------------------

>
> > OK, I was too early to praise Gerard. The following version:

>
> > def myround(x):
> > n = int(x)
> > if abs(x - n) >= 0.5 and n % 2:
> > return n + 1 - 2 * int(n<0)
> > else:
> > return n

>
> > of Gerard doesn't work for 0.6 (or 0.7, etc.) It gives the answer 0
> > but I would expect 1.0 ( because 0.6 doesn't end in 0.5 at all... so
> > usual rules of round( ) apply)

>
> Interestingly, you could solve this by using python 3.
> round(x[, n])
> Return the floating point value x rounded to n digits after the
> decimal point. If n is omitted, it defaults to zero. 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, for example, both round(0.5) and round(-0.5) are 0, and
> round(1.5) is 2). Delegates to x.__round__(n).
>
> My turn: ;-)
>
> def yaround(x):
> i = int(x)
> f = x - i
> if f != 0.5 and f != -0.5: return round(x)
> return 2.0*round(x/2.0)
>
> a = (-10.0, -9.0, -8.0, -4.6, -4.5, -4.4, -4.0, -3.6, -3.5,
> -3.4, -0.6, -0.5, -0.4, 0.0, 0.4, 0.5, 0.6, 0.9, 1.0,
> 1.4, 1.5, 1.6, 2.0, 2.4, 2.5, 2.6, 10.0, 100.0)
>
> b = (-10.0, -9.0, -8.0, -5.0, -4.0, -4.0, -4.0, -4.0, -4.0,
> -3.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
> 1.0, 2.0, 2.0, 2.0, 2.0, 2.0, 3.0, 10.0, 100.0)
>
> for i in range(len(a)):
> assert yaround(a) == b

Shorter version:
def round3k(x):
return x % 1 != 0.5 and round(x) or round(x / 2.) * 2.

nums = [ 0, 2, 3.2, 3.6, 3.5, 2.5, -0.5, -1.5, -1.3, -1.8, -2.5, 0.6,
0.7 ]
rnums = [ 0, 2, 3.0, 4.0, 4.0, 2.0, -0.0, -2.0, -1.0, -2.0, -2.0, 1.0,
1.0 ]

for num, rnum in zip(nums, rnums):
assert even_round(num) == rnum, '%s != %s' % (even_round(num),
rnum)
print num, '->', even_round(num)

It makes sense to add `from __future__ import even_round` to Python
2.6.

Ivan Illarionov, Apr 11, 2008
17. ### hdanteGuest

On Apr 11, 11:13 am, Ivan Illarionov <>
wrote:
>
> Shorter version:
> def round3k(x):
> return x % 1 != 0.5 and round(x) or round(x / 2.) * 2.

Strangely, a "faster" version is:

def fast_round(x):
if x % 1 != 0.5: return round(x)
return 2.0*round(x/2.0)

>
> nums = [ 0, 2, 3.2, 3.6, 3.5, 2.5, -0.5, -1.5, -1.3, -1.8, -2.5, 0.6,
> 0.7 ]
> rnums = [ 0, 2, 3.0, 4.0, 4.0, 2.0, -0.0, -2.0, -1.0, -2.0, -2.0, 1.0,
> 1.0 ]

You shouldn't remove assertions in the smaller version.

>
> for num, rnum in zip(nums, rnums):
> assert even_round(num) == rnum, '%s != %s' % (even_round(num),
> rnum)
> print num, '->', even_round(num)
>
> It makes sense to add `from __future__ import even_round` to Python
> 2.6.

hdante, Apr 11, 2008
18. ### Graham BreedGuest

On Apr 11, 6:14 pm, bdsatish <> wrote:
> The built-in function round( ) will always "round up", that is 1.5 is
> rounded to 2.0 and 2.5 is rounded to 3.0.
>
> If I want to round to the nearest even, that is
>
> my_round(1.5) = 2 # As expected
> my_round(2.5) = 2 # Not 3, which is an odd num

If you care about such details, you may be better off using decimals

> I'm interested in rounding numbers of the form "x.5" depending upon
> whether x is odd or even. Any idea about how to implement it ?

import decimal
decimal.Decimal("1.5").to_integral(
rounding=decimal.ROUND_HALF_EVEN)
decimal.Decimal("2.5").to_integral(
rounding=decimal.ROUND_HALF_EVEN)

ROUND_HALF_EVEN is the default, but maybe that can be changed, so
explicit is safest.

If you really insist,

import decimal
def my_round(f):
d = decimal.Decimal(str(f))
rounded = d.to_integral(rounding=decimal.ROUND_HALF_EVEN)
return int(rounded)

Graham

Graham Breed, Apr 11, 2008
19. ### Mikael OlofssonGuest

commented about rounding towards even numbers
from mid-way between integers as opposed to for instance always rounding
up in those cases:
> Strange request though, why do you need it that way, because 2.5 is
> CLOSER to 3 than to 2...

That's exactly how I was taught to do rounding in what-ever low-level
class it was. The idea is to avoid a bias, which assumes that the
original values are already quantized. Assume that we have values
quantized to one decimal only, and assume that all values of this
decimal are equally likely. Also assume that the integer part of our
numbers are equally likely to be even or odd. Then the average rounding
error when rounding to integers will be 0.05 if you always round up when
the decimal is 5. If you round towards an even number instead when the
decimal is 5, then you will round up half of those times, and round down
the other half, and the average rounding error will be 0. That's the
idea. Of course you could argue that it would be even more fair to make
the choice based on the tossing of a fair coin.

Note that if you do not have quantized values and assuming that the
fraction part is evenly distributed between 0 and 1, than this whole
argument is moot. The probability of getting exactly 0.5 is zero in that
case, just as the probability of getting any other specified number is zero.

That said, measurements are in practice always quantized, and rounding
towards an even number when mid-way between avoids an average error of
half the original precision.

As a side-note: The the smallest coin in Swedish currency SEK is 0.50,
but prices in stores are given with two decimals, i.e. with precision
0.01. But what if your goods add up to 12.34? The standard in Swedish
stores, after adding the prices of your goods, is to round the number to
the nearest whole or half SEK, which means that decimals 25 and 75 are
mid-way between. In those cases the rounding is usually done to the
nearest whole SEK, which is based on precicely the above reasoning. If
they did not do that, I could argue that they are stealing on average
0.005 SEK from me every time I go to the store. Well... I could live
with that, since 0.005 SEK is a ridiculously small amount, and even if I
make thousands of such transactions per year, it still sums up to a
neglectable amount.

Another side-note: My 12-year old son is now being taught to always
round up from mid-way between. Yet another example of the degradation of
maths in schools.

/MiO

Mikael Olofsson, Apr 11, 2008
20. ### Robert KernGuest

wrote:

> Strange request though, why do you need it that way, because 2.5 is
> CLOSER to 3 than to 2...

Uhhh, no it isn't. (3 - 2.5) == (2.5 - 2)

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma