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.
- Ada
- C
- Clojure
- C++
- C#
- D
- Dart
- Elixir
- Fortran
- Go
- Haskell
- JS
- Java
- Java
- Lisp
- Lua
- Lua
- PHP
- Pascal
- Perl
- Python
- Python
- Ruby
- Rust
- Scheme
Y : constant Integer := Integer (Float'Rounding (X));
y = Kernel.round x
Kernel.round tales integers or floats but always returns an integer, unlike Float.round which always returns a float
integer :: y
y = nint(x)
y = floor (x + 1/2)
Haskell would round half-ties towards nearest ∈ℤ2 rather than towards +∞
var y = Math.round(x);
import static java.lang.Integer.parseInt;
import static java.math.RoundingMode.HALF_UP;
import static java.text.NumberFormat.getNumberInstance;
NumberFormat f = getNumberInstance();
f.setRoundingMode(HALF_UP);
f.setMaximumFractionDigits(0);
int y = parseInt(f.format(x));
(defun rnd (y) (floor (+ y 0.5)))
function round(float)
local int, part = math.modf(float)
if float == math.abs(float) and part >= .5 then return int+1 -- positive float
elseif part <= -.5 then return int-1 -- negative float
end
return int
end
math.floor(x) will return the biggest integer below x
function round(float)
return math.floor(float + .5)
end
var
y: integer;
x: double;
begin
y := round(x);
end.
my $y = int($x + 1/2);
y = int(x + 0.5)
c = Context(rounding=ROUND_HALF_UP)
y = round(Decimal(x, c))
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.
(define y (round x))
Similar functions:
- round: compat with IEEE floating point rounding rules; rounds to even when halfway b/w two integers
- floor: largest int not larger than x
- ceiling: smallest int not smaller than x
- truncate: integer closest to x whose absolute value is not larger than the absolute value of x
- round: compat with IEEE floating point rounding rules; rounds to even when halfway b/w two integers
- floor: largest int not larger than x
- ceiling: smallest int not smaller than x
- truncate: integer closest to x whose absolute value is not larger than the absolute value of x