257

I was just re-readingWhat’s New In Python 3.0 and it states:

Theround() function rounding strategy and return type have changed.Exact halfway cases are now rounded to the nearest even result insteadof away from zero. (For example,round(2.5) now returns 2 rather than3.)

andthe documentation forround():

For the built-in types supportinground(), values are rounded to theclosest multiple of 10 to the power minus n; if two multiples areequally close, rounding is done toward the even choice

So, inPython 2 (for example, v2.7.3) I get the expected:

round(2.5)3.0round(3.5)4.0

However, now underPython 3 (for example v3.2.3):

round(2.5)2round(3.5)4

This seems counter-intuitive and contrary to what I understand aboutrounding (and bound to trip up people). English isn't my native language but until I read this I thought I knew what rounding meant :-/ I am sureat the time Python 3 was introduced there must have been some discussion ofthis, but I was unable to find a good reason in my search.

  1. Does anyone have insight into why this was changed to this?
  2. Are there any other mainstream programming languages (e.g.,C, C++, Java, Perl, ..) that do this sort of (to me inconsistent) rounding?

What am I missing here?

UPDATE: @Li-aungYip's comment re "Banker's rounding" gave me the right search term/keywords to search for and I found this SO question:Why does .NET use banker's rounding as default?, so I will be reading that carefully.

wjandrea's user avatar
wjandrea
34k10 gold badges69 silver badges106 bronze badges
askedMay 31, 2012 at 0:11
Levon's user avatar
13
  • 34
    I don't have time to look this up, but I believe this is called "Banker's rounding". I believe it's common in the finance industry.CommentedMay 31, 2012 at 0:21
  • 3
    @sberry well, yes, its behavior is consistent with its own description. So if it would say "rounding" is doubling its value and did it, it would also be consistent :) .. but it seems contrary to what rounding commonlymeans. So I'm looking for a better understanding.CommentedMay 31, 2012 at 0:23
  • 7
    Just a note: Bankers rounding isn't common just in finance. This is how I was taught to round in elementary school already in the 70's :-)CommentedMar 11, 2013 at 7:46
  • 5
    Boy it would be nice ifround() could just accept a separate argument to change the rounding behavior.CommentedMar 30, 2022 at 21:55
  • 2
    @SirajAlam, that's because340.33 does not exist in floating point arithmetic. When you see340.33 you're actually getting340.33499999999997953636921010911464691162109375 which of course does round to340.33 even under bankers rounding. Python puts in extra effort to display340.33 even though it knows it isn't because it thinks that's what you want to see:docs.python.org/3/tutorial/floatingpoint.html To find out what number you really have when you enter a float you canfrom decimal import Decimal and thenDecimal(340.33).CommentedMar 17, 2023 at 0:03

13 Answers13

250

Python 3's way (called "round half to even" or "banker's rounding") is considered the standard rounding method these days, though some language implementations aren't on the bus yet.

The simple "always round 0.5 up" technique results in a slight bias toward the higher number. With large numbers of calculations, this can be significant. The Python 3 approach eliminates this issue.

There is more than one method of rounding in common use. IEEE 754, the international standard for floating-point math, definesfive different rounding methods (the one used by Python 3 is the default). Andthere are others.

This behavior is not as widely known as it ought to be. AppleScript was, if I remember correctly, an early adopter of this rounding method. Theround command in AppleScript offers several options, but round-toward-even is the default as it is in IEEE 754. Apparently the engineer who implemented theround command got so fed up with all the requests to "make it work like I learned in school" that he implemented just that:round 2.5 rounding as taught in school is a valid AppleScript command. :-)

wjandrea's user avatar
wjandrea
34k10 gold badges69 silver badges106 bronze badges
answeredMay 31, 2012 at 0:24
kindall's user avatar
Sign up to request clarification or add additional context in comments.

17 Comments

I wasn't aware of this "default standard rounding method pretty much universally these days", would you (or anyone else) know if C/C++/Java/Perl or any other "main-stream" languages implement rounding the same way?
Ruby does it. Microsoft's .NET languages do it. Java doesn't appear to, though. I can't track it down for every possible language, but I guess it's most common in fairly recently-designed languages. I imagine C and C++ are old enough that they don't.
ruby returns3 for2.5.round
I added a bit about AppleScript's handling of this because I love the sarcastic way the "old" behavior is implemented.
@kindall This method has been the IEEE default rounding mode since 1985 (when IEEE 754-1985 was published). It has also been the default rounding mode in C since at least C89 (and thus also in C++),however, since C99 (and C++11 with sporadic support before that) a "round()" function has been available that uses ties round away from zero instead. Internal floating point rounding and the rint() family of functions still obey the rounding mode setting, which defaults to round ties to even.
|
58

You can control the rounding you get in Py3000 using theDecimal module:

>>> decimal.Decimal('3.5').quantize(decimal.Decimal('1'),     rounding=decimal.ROUND_HALF_UP)>>> Decimal('4')>>> decimal.Decimal('2.5').quantize(decimal.Decimal('1'),        rounding=decimal.ROUND_HALF_EVEN)>>> Decimal('2')>>> decimal.Decimal('3.5').quantize(decimal.Decimal('1'),     rounding=decimal.ROUND_HALF_DOWN)>>> Decimal('3')
Barthelemy's user avatar
Barthelemy
8,8776 gold badges35 silver badges37 bronze badges
answeredMay 31, 2012 at 1:59
dawg's user avatar

5 Comments

Thanks .. I was not familiar with this module. Any idea how I would get the behavior of Python v 2.x? The examples you show don't seem to do that. Just curious if that would be possible.
@Levon: The constantROUND_HALF_UP is the same as Python 2.X's old behavior.
You can also set a context for the Decimal module that does this for you implicitly. See thesetcontext() function.
This is exactly what I was looking for today. Working as expected in Python 3.4.3. Also worth noting, you can control how much it rounds by changingquantize(decimal.Decimal('1') toquantize(decimal.Decimal('0.00') if you want to round to nearest 100s such as for money.
This solution works as a replacement forround(number, ndigits) as long asndigits is positive, but annoyingly you cannot use it to replace something likeround(5, -1).
23

Just to add here an important note from documentation:

https://docs.python.org/dev/library/functions.html#round

Note

The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

So don't be surprised to get following results in Python 3.2:

>>> round(0.25,1), round(0.35,1), round(0.45,1), round(0.55,1)(0.2, 0.3, 0.5, 0.6)>>> round(0.025,2), round(0.035,2), round(0.045,2), round(0.055,2)(0.03, 0.04, 0.04, 0.06)
answeredJul 2, 2014 at 14:54
skif1979's user avatar

5 Comments

I saw that. And my first reaction: Who is using a 16-bit CPU that is incapable of representing all permutations of "2.67x" ? Saying that fractions can't be expressed in float seems like a scapegoat here: no modern CPU is that inaccurate, in ANY langauge (except Python?)
@Adam: I think you're misunderstanding. The binary format (IEEE 754 binary64) used to store floats can't represent2.675 exactly: the closest the computer can get is2.67499999999999982236431605997495353221893310546875. That's pretty close, but it's notexactly equal to2.675: it'svery slightly closer to2.67 than to2.68. So theround function does the right thing, and rounds it to the closer 2-digit-after-the-point value, namely2.67. This has nothing to do with Python, and everything to do with binary floating-point.
It's not "the right thing" because it was given a source-code constant :), but I see your point.
@Adam: I ran into this same quirkiness in JS before so it is not language specific.
@Adam it is the right thing. Python is very clear in its docs that it works with binary numbers by default, not with decimal numbers. The ability to enter literals in decimal format does not change anything about that. The float for one tenth simply does not exist in Python (or most other programming languages). However, you can use thefractions module or thedecimal module in the Python standard library to do exact calculations with fractions and decimal numbers.
13

Python 3.x rounds .5 values to a neighbour which is even

assert round(0.5) == 0assert round(1.5) == 2assert round(2.5) == 2import decimalassert decimal.Decimal('0.5').to_integral_value() == 0assert decimal.Decimal('1.5').to_integral_value() == 2assert decimal.Decimal('2.5').to_integral_value() == 2

however, one can change decimal rounding "back" to always round .5 up, if needed :

decimal.getcontext().rounding = decimal.ROUND_HALF_UPassert decimal.Decimal('0.5').to_integral_value() == 1assert decimal.Decimal('1.5').to_integral_value() == 2assert decimal.Decimal('2.5').to_integral_value() == 3i = int(decimal.Decimal('2.5').to_integral_value()) # to get an intassert i == 3assert type(i) is int
answeredOct 2, 2018 at 11:29
kares's user avatar

Comments

8

I recently had problems with this, too. Hence, I have developed a python 3 module that has 2 functionstrueround() andtrueround_precision() that address this and give the same rounding behaviour we are used to from primary school (not banker's rounding). Here is the module. Just save the code and copy it in or import it.

Note: the trueround_precision module can change the rounding behaviour depending on needs according to the ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN, ROUND_HALF_UP, ROUND_UP, and ROUND_05UP flags in the decimal module (see that modules documentation for more info). For the functions below, see the docstrings or usehelp(trueround) andhelp(trueround_precision) if copied into an interpreter for further documentation.

#! /usr/bin/env python3# -*- coding: utf-8 -*-def trueround(number, places=0):    '''    trueround(number, places)        example:                >>> trueround(2.55, 1) == 2.6        True    uses standard functions with no import to give "normal" behavior to     rounding so that trueround(2.5) == 3, trueround(3.5) == 4,     trueround(4.5) == 5, etc. Use with caution, however. This still has     the same problem with floating point math. The return object will     be type int if places=0 or a float if places=>1.        number is the floating point number needed rounding        places is the number of decimal places to round to with '0' as the        default which will actually return our interger. Otherwise, a        floating point will be returned to the given decimal place.        Note:   Use trueround_precision() if true precision with            floats is needed    GPL 2.0    copywrite by Narnie Harshoe <[email protected]>    '''    place = 10**(places)    rounded = (int(number*place + 0.5if number>=0 else -0.5))/place    if rounded == int(rounded):        rounded = int(rounded)    return roundeddef trueround_precision(number, places=0, rounding=None):    '''    trueround_precision(number, places, rounding=ROUND_HALF_UP)        Uses true precision for floating numbers using the 'decimal' module in    python and assumes the module has already been imported before calling    this function. The return object is of type Decimal.    All rounding options are available from the decimal module including     ROUND_CEILING, ROUND_DOWN, ROUND_FLOOR, ROUND_HALF_DOWN, ROUND_HALF_EVEN,     ROUND_HALF_UP, ROUND_UP, and ROUND_05UP.    examples:                >>> trueround(2.5, 0) == Decimal('3')        True        >>> trueround(2.5, 0, ROUND_DOWN) == Decimal('2')        True    number is a floating point number or a string type containing a number on         on which to be acted.    places is the number of decimal places to round to with '0' as the default.    Note:   if type float is passed as the first argument to the function, it            will first be converted to a str type for correct rounding.    GPL 2.0    copywrite by Narnie Harshoe <[email protected]>    '''    from decimal import Decimal as dec    from decimal import ROUND_HALF_UP    from decimal import ROUND_CEILING    from decimal import ROUND_DOWN    from decimal import ROUND_FLOOR    from decimal import ROUND_HALF_DOWN    from decimal import ROUND_HALF_EVEN    from decimal import ROUND_UP    from decimal import ROUND_05UP    if type(number) == type(float()):        number = str(number)    if rounding == None:        rounding = ROUND_HALF_UP    place = '1.'    for i in range(places):        place = ''.join([place, '0'])    return dec(number).quantize(dec(place), rounding=rounding)
Bakr's user avatar
Bakr
3972 silver badges12 bronze badges
answeredDec 20, 2012 at 0:31
narnie's user avatar

Comments

4

Python 2 rounding behaviour in python 3.

Adding 1 at the 15th decimal places.Accuracy upto 15 digits.

round2=lambda x,y=None: round(x+1e-15,y)

Not right for 175.57. For that it should be added in the 13th decimal place as the number is grown. Switching to Decimal is better than reinventing the same wheel.

from decimal import Decimal, ROUND_HALF_UPdef round2(x, y=2):    prec = Decimal(10) ** -y    return float(Decimal(str(round(x,3))).quantize(prec, rounding=ROUND_HALF_UP))

Not used y

answeredJul 3, 2017 at 14:55
Smart Manoj's user avatar

4 Comments

Could you explain the intuition behind this formula?
From what I understand, fractions that can't be accurately represented will have up to 15 9's, then the imprecision. For example,2.675 is2.67499999999999982236431605997495353221893310546875. Adding 1e-15 will tip it over 2.675 and get it rounded correctly. if the fraction is already over the code constant, adding 1e-15 will change nothing to the rounding.
nice trick also works for3.46//0.01==345 but(3.46+1E-15)//0.01==346 as wanted
Are there cases where this would prevent correct rounding down? I mean other than the occasion where the true number is exactly x.xxx9999999999999, in which case you couldn't know for sure if the 9s stop or continue because this is max precision for a common float64, actually slightly beyond float64 depending on which direction you are converting bi-dec-bi or dec-bi-dec and in which numeral system you need to retain the accuracy. (All assuming no outside confirmation calulations with true fractions or arbitrary precision.)
2

Some cases:

in: Decimal(75.29 / 2).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)in: round(75.29 / 2, 2)out: 37.65 GOODin: Decimal(85.55 / 2).quantize(Decimal('0.01'), rounding=ROUND_HALF_UP)in: round(85.55 / 2, 2)out: 42.77 BAD

For fix:

in: round(75.29 / 2 + 0.00001, 2)out: 37.65 GOODin: round(85.55 / 2 + 0.00001, 2)out: 42.78 GOOD

If you want more decimals, for example 4, you should add (+ 0.0000001).

Work for me.

answeredNov 7, 2018 at 8:30
Virako's user avatar

1 Comment

This was the only solution that worked for me, thanks for posting. Everyone seems to be intent on 0.5 rounding up/down, so I couldn't manage multi decimal rounding issues.
0

Sample Reproduction:

['{} => {}'.format(x+0.5, round(x+0.5)) for x in range(10)]['0.5 => 0', '1.5 => 2', '2.5 => 2', '3.5 => 4', '4.5 => 4', '5.5 => 6', '6.5 => 6', '7.5 => 8', '8.5 => 8', '9.5 => 10']

API:https://docs.python.org/3/library/functions.html#round

States:

Return number rounded to ndigits precision after the decimal point. 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). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as number.

For a general Python object number, round delegates to number.round.

Note The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

Given this insight you can use some math to resolve it

import mathdef my_round(i):  f = math.floor(i)  return f if i - f < 0.5 else f+1

now you can run the same test with my_round instead of round.

['{} => {}'.format(x + 0.5, my_round(x+0.5)) for x in range(10)]['0.5 => 1', '1.5 => 2', '2.5 => 3', '3.5 => 4', '4.5 => 5', '5.5 => 6', '6.5 => 7', '7.5 => 8', '8.5 => 9', '9.5 => 10']
answeredJan 14, 2019 at 22:34
Fallenreaper's user avatar

Comments

0

I propose custom function which would work for a DataFrame:

def dfCustomRound(df, dec):    d = 1 / 10 ** dec    df = round(df, dec + 2)    return (((df % (1 * d)) == 0.5 * d).astype(int) * 0.1 * d * np.sign(df) + df).round(dec)
answeredOct 7, 2022 at 8:50
Dmitry Korotkov's user avatar

Comments

0

without Decimal,

def my_round(x: float, precision: int = 2):    tmp = round(x, precision)    if x == tmp + float('0.' + '0' * precision + '5'):        return round(x + float('0.' + '0' * precision + '1'), precision)    return tmp
answeredApr 28, 2024 at 3:51
dogdog's user avatar

Comments

-2
# round module within numpy when decimal is X.5 will give desired (X+1)import numpy as npexample_of_some_variable = 3.5rounded_result_of_variable = np.round(example_of_some_variable,0)print (rounded_result_of_variable)
Eric Aya's user avatar
Eric Aya
70.2k36 gold badges190 silver badges266 bronze badges
answeredJul 4, 2022 at 9:59
Leon D'Cruz's user avatar

2 Comments

No, it doesn't!
print(np.round(2.5,0)) prints2.0, as expected for the IEEE-754 default FP rounding mode, which is nearest with even as a tie-break, not away-from-zero or the towards +Inf you'r claiming.
-3

Try this code:

def roundup(input):      demo = input  if str(input)[-1] != "5" else str(input).replace("5","6")   place = len(demo.split(".")[1])-1   return(round(float(demo),place))

The result will be:

>>> x = roundup(2.5)>>> x3.0  >>> x = roundup(2.05)>>> x2.1 >>> x = roundup(2.005)>>> x2.01

Ooutput you can check here:https://i.sstatic.net/QQUkS.png

Ṃųỻịgǻňạcểơửṩ's user avatar
Ṃųỻịgǻňạcểơửṩ
2,6378 gold badges27 silver badges36 bronze badges
answeredJan 2, 2020 at 17:03
Anil Kumar Sahoo's user avatar

Comments

-7

You can control the rounding you using the math.ceil module:

import mathprint(math.ceil(2.5))> 3
answeredFeb 14, 2020 at 15:44
Eds_k's user avatar

5 Comments

That will always return the number without its decimal part, this is not rounding. ceil(2.5) = 2, ceil(2.99) = 2
in python3+, If the number argument is a positive or negative number, the ceil function returns the ceiling value.
In [14]: math.ceil(2.99) Out[14]: 3
Yes, I'm sorry I was wrong. Ceil() returns the ceiling value whereas floor() returns the one I was talking, about. But still, in my opinion this is not quite the rounding behaviour (both these functions)
@krafter: Just for the record, stripping away the fractional part is called "truncation", rounding towards 0.0. e.g.math.trunc(3.9) is3,math.trunc(-3.9) is-3.floor rounds towards -Inf,ceil rounds towards +Infinity.

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.