Logo

Programming-Idioms

History of Idiom 81 > diff from v20 to v21

Edit summary for version 21 by :
New Python implementation by user [TinyFawks]

Version 20

2016-01-15, 15:42:48

Version 21

2016-02-18, 09:18:33

Idiom #81 Round floating point number to integer

Declare integer y and initialize it with the rounded value of floating point number x .
Ties (when the fractional part of x is exactly .5) must be rounded up (to positive infinity).

Idiom #81 Round floating point number to integer

Declare integer y and initialize it with the rounded value of floating point number x .
Ties (when the fractional part of x is exactly .5) must be rounded up (to positive infinity).

Code
y = math.ceil(a) if a - int(a) >= .5 else int(a)
Comments bubble
python 3 round() uses "to nearest, ties to even"
python 2.7 round() uses "to nearest, ties to positive infinity"