www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/ControllingNumberDisplayWhileRoundingExample.m
%% Controlling Number Display While Rounding %% % The |format| command controls how MATLAB(R) displays numbers at the % command line. If a number has extra digits that cannot be displayed in % the current format, then MATLAB automatically rounds the number for % display purposes. This can lead to unexpected results when combined with % the |round| function. % % Consider the result of the following subtraction operation, which % displays 5 digits. format short x = 112.05 - 110 %% % Based on the displayed value of |x|, rounding |x| to 1 decimal should % return |2.1|. round(x,1) %% % In fact, the problem here is that MATLAB is rounding |x| to 5 digits for % display purposes. The |round| function returns the correct answer. % Confirm the answer by viewing |x| with |format long|, which displays |x| % rounded to 15 digits. format long x