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...

round2ev(X)
function Y = round2ev(X)
% Round towards the nearest integer (unbiased rounding / round half to even)
%
% (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 to the nearest even integer.
% For complex X, the imaginary and real parts are rounded independently.
%
% Syntax:
%  Y = round2ev(X)
%
% See also ROUND2OD ROUND2DN ROUND2UP ROUND2RA ROUND2ZE ROUND2SF ROUND2DP PREFNUM DATEROUND ROUND CONVERGENT
%
% Note 1: Known as unbiased rounding, convergent rounding, Dutch rounding,
%         Gaussian rounding, statistician's rounding or bankers' rounding.
% Note 2: MATLAB's Fixed-Point toolbox provides "convergent".
% Note 3: Defined by ISO/IEC/IEEE 60559 (originally IEEE 754).

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

Contact us