2012-05-31 00:11:09 8 Comments

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

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

and the documentation for round:

For the built-in types supporting round(), 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, under **v2.7.3**:

```
In [85]: round(2.5)
Out[85]: 3.0
In [86]: round(3.5)
Out[86]: 4.0
```

as I'd have expected. However, now under **v3.2.3**:

```
In [32]: round(2.5)
Out[32]: 2
In [33]: round(3.5)
Out[33]: 4
```

This seems counter-intuitive and contrary to what I understand about rounding (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 sure at the time v3 was introduced there must have been some discussion of this, but I was unable to find a good reason in my search.

- Does anyone have insight into why this was changed to this?
- 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.

### Related Questions

#### Sponsored Content

#### 33 Answered Questions

#### 21 Answered Questions

### [SOLVED] Does Python have a ternary conditional operator?

**2008-12-27 08:32:18****Devoted****1731284**View**5377**Score**21**Answer- Tags: python operators ternary-operator conditional-operator

#### 25 Answered Questions

### [SOLVED] How can I safely create a nested directory?

**2008-11-07 18:56:45****Parand****2348712**View**3706**Score**25**Answer- Tags: python exception path directory operating-system

#### 28 Answered Questions

#### 69 Answered Questions

### [SOLVED] Round to at most 2 decimal places (only if necessary)

**2012-08-06 17:17:48****stinkycheeseman****2065550**View**2342**Score**69**Answer- Tags: javascript decimal rounding decimal-point

#### 16 Answered Questions

### [SOLVED] What are metaclasses in Python?

**2008-09-19 06:10:46****e-satis****722072**View**5265**Score**16**Answer- Tags: python oop metaclass python-datamodel

#### 57 Answered Questions

### [SOLVED] Calling an external command in Python

**2008-09-18 01:35:30****freshWoWer****3136079**View**4391**Score**57**Answer- Tags: python shell command subprocess external

#### 20 Answered Questions

### [SOLVED] What is the difference between Python's list methods append and extend?

**2008-10-31 05:55:36****Claudiu****2731856**View**3119**Score**20**Answer- Tags: python list data-structures append extend

## 9 comments

## @Asad Manzoor 2019-02-06 06:34:22

The

roundoperator will round off the value to nearest integer value.For Example:If value is greater than o.5 than it will round off to 1

If value is less than 0.5 than it will round off to 0

## @Heikki 2019-02-06 07:12:03

`//`

operator is not used for rounding, see stackoverflow.com/questions/1535596/…## @Fallenreaper 2019-01-14 22:34:55

Sample Reproduction:

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

States:

Given this insight you can use some math to resolve it

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

## @Virako 2018-11-07 08:30:49

Some cases:

For fix:

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

Work for me.

## @Gayathri 2019-08-05 21:36:41

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.

## @kares 2018-10-02 11:29:25

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

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

## @kindall 2012-05-31 00:24:15

Python 3.0's way 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.0 approach eliminates this issue.

There is more than one method of rounding in common use. IEEE 754, the international standard for floating-point math, defines five different rounding methods (the one used by Python 3.0 is the default). And there 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. The

`round`

command in AppleScript actually does offer several options, but round-toward-even is the default as it is in IEEE 754. Apparently the engineer who implemented the`round`

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. :-)## @Levon 2012-05-31 00:26:42

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?

## @kindall 2012-05-31 00:35:52

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.

## @jfs 2012-05-31 00:50:50

ruby returns

`3`

for`2.5.round`

## @kindall 2012-05-31 01:31:26

Ah, I think the page I found must have been for IronRuby, then, which would inherit the Microsoft CLR rounding behavior.

## @jfs 2012-05-31 01:36:18

round() function from Math::Round in Perl also produces

`3`

, though`printf "%.f", 2.5`

produces expected`2`

.## @kindall 2012-05-31 01:37:54

I added a bit about AppleScript's handling of this because I love the sarcastic way the "old" behavior is implemented.

## @Levon 2012-05-31 14:30:48

@kindall good stuff, thanks for all the info. (At the risk of dating myself, I used to speak 6502 at some time too :)

## @Wlerin 2015-11-22 21:41:59

@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.## @limonik 2019-02-04 15:17:24

As I learned, " in Python 3, decimals are rounded to the nearest even number." I am using Python 3.6.6. When I enter round(17.2), I expected to see 18 but I saw 17. For Python 3.6.6 is there another rule?

## @kindall 2019-02-04 16:18:52

The rule applies only to fractional parts of 0.5, sorry if that wasn't clear. 17.2 is going to round to 17.0.

## @Ivaylo Strandjev 2019-04-09 13:10:00

The scariest about this behavior is that there are now cases where

`round(x + 1) - round(x)`

might be 0 or 2.## @SmartManoj 2017-07-03 14:55:40

Python 2 rounding behaviour in python 3.

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

## @Hadi 2017-08-18 14:32:35

Could you explain the intuition behind this formula?

## @Benoit Dufresne 2018-08-08 17:17:22

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`

is`2.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.## @dawg 2012-05-31 01:59:12

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

## @Levon 2012-05-31 02:05:55

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.

## @dawg 2012-05-31 02:10:17

@Levon: The constant

`ROUND_HALF_UP`

is the same as Python 2.X's old behavior.## @kindall 2012-05-31 21:04:23

You can also set a context for the Decimal module that does this for you implicitly. See the

`setcontext()`

function.## @Igor 2017-02-01 18:14:59

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 changing

`quantize(decimal.Decimal('1')`

to`quantize(decimal.Decimal('0.00')`

if you want to round to nearest 100s such as for money.## @Pekka Klärck 2018-06-15 11:39:00

This solution works as a replacement for

`round(number, ndigits)`

as long as`ndigits`

is positive, but annoyingly you cannot use it to replace something like`round(5, -1)`

.## @skif1979 2014-07-02 14:54:47

Just to add here an important note from documentation:

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

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

## @Adam 2015-11-30 17:24:49

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

## @Mark Dickinson 2016-08-05 13:46:08

@Adam: I think you're misunderstanding. The binary format (IEEE 754 binary64) used to store floats can't represent

`2.675`

exactly: the closest the computer can get is`2.67499999999999982236431605997495353221893310546875`

. That's pretty close, but it's notexactlyequal to`2.675`

: it'svery slightlycloser to`2.67`

than to`2.68`

. So the`round`

function does the right thing, and rounds it to the closer 2-digit-after-the-point value, namely`2.67`

. This has nothing to do with Python, and everything to do with binary floating-point.## @Adam 2016-08-07 19:52:59

It's not "the right thing" because it was given a source-code constant :), but I see your point.

## @Igor 2017-02-23 23:38:35

@Adam: I ran into this same quirkiness in JS before so it is not language specific.

## @narnie 2012-12-20 00:31:32

I recently had problems with this, too. Hence, I have developed a python 3 module that has 2 functions trueround() and trueround_precision() that address this and give the same rounding behaviour were 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 use help(trueround) and help(trueround_precision) if copied into an interpreter for further documentation.

Hope this helps,

Narnie