Logo

Programming-Idioms

History of Idiom 81 > diff from v21 to v22

Edit summary for version 22 by :
New Pascal implementation by user [Bart]

Version 21

2016-02-18, 09:18:33

Version 22

2016-02-18, 16:58:01

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"
Imports
import "math"
Imports
import "math"
Code
y := int(math.Floor(x + 0.5))
Code
y := int(math.Floor(x + 0.5))
Demo URL
http://play.golang.org/p/mWWdERPVix
Demo URL
http://play.golang.org/p/mWWdERPVix