Round to nearest decimal or integer – MATLAB round

The format command controls how MATLAB® 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 display can lead to unexpected results when combined with the round function.

Consider the result of this subtraction operation, which displays 5 digits.

format 

short

x = 112.05 - 110
x = 2.0500

The displayed result is 2.0500, which looks like a tie. However, due to the floating-point arithmetic error, the tie at a fractional part of 0.5 is not within roundoff error.

Based on the displayed value of x, rounding x to 1 decimal should return 2.1.

y = round(x,1)
y = 2

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
x = 
   2.049999999999997

For comparison, show the rounding results for a tie that is within roundoff error and for a tie that is not within roundoff error.

x1 = 2.05
x1 = 
   2.050000000000000

y1 = round(x1,1)
y1 = 
   2.100000000000000

x2 = 2.05 - eps(2.05)
x2 = 
   2.049999999999999

y2 = round(x2,1)
y2 = 
     2