matlab函数对比round(最近取整)和fix(向0取整)

本文最后更新于 2025年11月18日。

round是取最近的整数

>> help round
 round  rounds towards nearest decimal or integer
 
    round(X) rounds each element of X to the nearest integer.
    
    round(X, N), for positive integers N, rounds to N digits to the right
    of the decimal point. If N is zero, X is rounded to the nearest integer.
    If N is less than zero, X is rounded to the left of the decimal point.
    N must be a scalar integer.
 
    round(X, N, 'significant') rounds each element to its N most significant
    digits, counting from the most-significant or left side of the number. 
    N must be a positive integer scalar.
 
    round(X, N, 'decimals') is equivalent to round(X, N).
 
    For complex X, the imaginary and real parts are rounded independently.
 
    Examples
    --------
    % Round pi to the nearest hundredth
    >> round(pi, 2)
         3.14
 
    % Round the equatorial radius of the Earth, 6378137 meters,
    % to the nearest kilometer.
    round(6378137, -3)
         6378000
 
    % Round to 3 significant digits
    format shortg;
    round([pi, 6378137], 3, 'significant')
         3.14     6.38e+06
 
    If you only need to display a rounded version of X,
    consider using fprintf or num2str:
 
    fprintf('%.3f\n', 12.3456)
         12.346 
    fprintf('%.3e\n', 12.3456)
         1.235e+01
 
   See also floor, ceil, fprintf.

    Reference page for round
    Other functions named round

fix是向0取整

>> help fix
 fix    Round towards zero.
    fix(X) rounds the elements of X to the nearest integers
    towards zero.
 
    See also floor, round, ceil.

    Reference page for fix
    Other functions named fix

实例

>> round(0.8)

ans =

     1

>> round(-0.8)

ans =

    -1

>> fix(0.8)

ans =

     0

>> fix(-0.8)

ans =

     0