Logo

Programming-Idioms

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).
Implementation
Lua

Implementation edit is for fixing errors and enhancing with metadata. Please do not replace the code below with a different implementation.

Instead of changing the code of the snippet, consider creating another Lua implementation.

Be concise.

Be useful.

All contributions dictatorially edited by webmasters to match personal tastes.

Please do not paste any copyright violating material.

Please try to avoid dependencies to third-party libraries and frameworks.

Other implementations
long y = Math.round(x);
y = (x + 1/2r).floor
import std.math: round;
int y = cast(int) x.round;
y = floor (x + 1/2)
import "math"
y := int(math.Floor(x + 0.5))
var
  y: integer;
  x: double;
begin
  y := round(x);
end.
#include <math.h>
int y = (int)floorf(x + 0.5f);
#include <cmath>
int y = static_cast<int>(std::floor(x + 0.5f));
$y = (int) round($x);
my $y = int($x + 1/2);
let y = x.round() as i64;
y = Kernel.round x
var y = Math.round(x);
function round(float)
    return math.floor(float + .5)
end
y = int(x + 0.5)
using System;
long y = (long)Math.Round(x);
integer :: y

y = nint(x)
(define y (round x))
var y = x.round();
Y : constant Integer := Integer (Float'Rounding (X));
(defun rnd (y) (floor (+ y 0.5)))
(defn rnd [y] (int (+ y 0.5)))