Logo

Programming-Idioms

History of Idiom 81 > diff from v32 to v33

Edit summary for version 33 by programming-idioms.org:
[Ruby] 2 ways => 2 impls. +DemoURL. +BUG warning.

Version 32

2016-09-18, 15:33:44

Version 33

2016-11-13, 14:48:15

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 = x.round
y = Math.round(x)
Code
y = x.round
Comments bubble
BUG: this rounds -0.5 to -1 instead of 1.
Doc URL
http://ruby-doc.org/core-2.2.3/Float.html#method-i-round
Doc URL
http://ruby-doc.org/core-2.2.3/Float.html#method-i-round
Demo URL
https://repl.it/EWcu/1