Code covered by the BSD License  

Highlights from
Rounding Functions Collection

  • round2dn(X) Round towards the nearest integer (asymmetric rounding / round half down)
  • round2dp(X,DcP,FnH) Round a numeric to given decimal places. Choose any rounding function.
  • round2ev(X) Round towards the nearest integer (unbiased rounding / round half to even)
  • round2od(X) Round towards the nearest integer (unbiased rounding / round half to odd)
  • round2ra(X) Round towards the nearest integer (random rounding / stochastic rounding)
  • round2sf(X,SgF,FnH) Round a numeric to given significant figures. Choose any rounding function.
  • round2up(X) Round towards the nearest integer (asymmetric rounding / round half up)
  • round2ze(X) Round towards the nearest integer (symmetric rounding / round half towards zero)
  • View all files
image thumbnail
from Rounding Functions Collection by Stephen Cobeldick
All the rounding functions you will ever need: round to even, significant figures, decimal places...

round2dn(X)
function Y = round2dn(X)
% Round towards the nearest integer (asymmetric rounding / round half down)
%
% (c) 2013 Stephen Cobeldick
%
% Rounds the elements of X to the nearest integers. X may be an N-D matrix.
% Elements with a fraction of 0.5 round down to the next integer (towards -Inf).
% For complex X, the imaginary and real parts are rounded independently.
%
% Syntax:
%  Y = round2dn(X)
%
% See also ROUND2UP ROUND2EV ROUND2OD ROUND2RA ROUND2ZE ROUND2SF ROUND2DP PREFNUM DATEROUND ROUND
%
% Note 1: Known as asymmetric rounding or round half towards minus infinity.

if isreal(X)
    Z = rem(X,1);
    Z(Z~=0.5) = 0;
else
    R = rem(real(X),1);
    J = rem(imag(X),1);
    R(R~=0.5) = 0;
    J(J~=0.5) = 0;
    Z = complex(R,J);
end
Y = round(X-Z);
%----------------------------------------------------------------------End!

Contact us