Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Ruby

Idiom #81 Round floating point number to integer

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

y = (x + 1/2r).floor

y = x.round_ rounds halves away from zero. round_ takes a parameter to round halves toward zero, or toward nearest even (bankers rounding), but not one to satisfy this task.
Y : constant Integer := Integer (Float'Rounding (X));

New implementation...