Loren on the Art of MATLAB

Turn ideas into MATLAB

Rounding Results 15

Posted by Loren Shure,

There are frequent questions on the MATLAB newsgroup about rounding results to a certain number of decimal places. MATLAB itself doesn't provide this functionality explicitly, though it is easy to accomplish.


Sidetrack : A Little MathWorks History

MathWorks' first Massachusetts office phone number was 653-1415 (ignoring country and area codes). The astute reader will notice that the last 5 digits are an approximation for (or, in MATLAB, pi). A local resident called one day to say that she kept getting calls for MathWorks and she wasn't sure why. But it was quite inconvenient for her because she spent lots of time on the second floor of her home, and the phone was on the first floor. The excess round-trips were taxing her! To understand what was happening, you should know that in some of the early MathWorks materials, the phone number was listed as .


format long
x = 1.23456789
x =

Use round. Here we get no decimals at all.

ans =

There are many ways to get the number of decimals you want. Here's one way to round to 3 decimals.

ans =

Here's another way.

ans =

And another. In this case, using the "easy" way to specify the format, you need to know how many integral digits there are as well.

ans =

Tools for Rounding Solutions

There are a lot of tools for helping you round numbers. The functions I list here for MATLAB form the basis of many, if not all, of the specific solutions.


Mapping Toolbox

File Exchange Rounding Tools

% N= num2str(X,SF);
% N= str2num(N);

Question for You

When you round values, do you want this for display only, or wanted for calculations? Some applications I can think of might include processing data that has a smaller number of bits of precision to start with. What applications do you need rounding for in your calculations? Post here with your thoughts.

Get the MATLAB code

Published with MATLAB® 7.8


Comments are closed.

15 CommentsOldest to Newest

Ness replied on : 1 of 15

Dear Loren,

I have used the first option you mention several times, and it’s been always for the post-processing part and never for the calculations.


Cris Luengo replied on : 2 of 15

I’ve used things like ceil(x/1000)*1000 to calculate axis limits for pretty graphs. I think it’s nice when there’s a label at the top end of the axis, especially with logarithmic scaling.

Daniel replied on : 4 of 15

For display I allways use the *printf() version.
For calculations, the first method proposed is the only one I find reasonable, even if it doesn’t exactly give the most readable code.
Sometimes I use a method not presented here, and that is the typecast method. Of course, it only makes sense when you want to round to the classical number formats, but you are absolutely sure you get what you want.

Loren replied on : 7 of 15


Obviously you still need to be aware of numerical round-off in the calculations done before rounding in the first place. And using floor instead of round may not be what you want to do.


Craig Miller replied on : 8 of 15

I use rounding for calculations and comparisions with specification limits. For example if the lower spec limit is 1.8 and the calculated value is 1.78254343 I want this value to pass because it rounds to 1.8 which is not less than the specification limit. This may not be ‘correct’ but it’s the way we’ve done our spec checking since time immemorial (in computer terms anyway).

Loren replied on : 9 of 15


I’m glad rounding works for you. Are you rounding to tenths for your work? Is your target value always in in tenths? Another possibility is to check if the calculated value is within a certain distance from the spec (e.g., abs(spec-calc) < SomeTolerance). --Loren

OysterEngineer replied on : 10 of 15

The illustrates why MatLab needs a smarter round function, much like even lowly Excel has, where the user can specify the number of digits to which you want to round to.

Loren replied on : 11 of 15


I recommend you use the support link on the right to enter your suggestion for round. Suggestions directly from customers tend to carry more weight. Thanks.


Drazick replied on : 12 of 15

I agree with OysterEngineer.
Sometimes, because of finite accuracy of Matlab I get results such as 3E-16. I want it to be exactly 0. It would be nice to such function.

Though it can be done by round.


Loren replied on : 13 of 15

Drazick, OysterEngineer, and possibly others,

Because of the nature of floating point arithmetic, getting exactly zero every time is not possible unless perhaps you can control every aspect of the calculations, order, whether on the stack, etc. In which case, MATLAB is possibly not the best tool if you override all operations.


Mark replied on : 14 of 15

Why does MatLab not have a floating point rounding function to round to 1 – 6 places to the right of the decimal point?

I had to write my own and using the basic (x*10^n/10^n) where n is the number of decimal places does not work reliably (round numbers like 3.44545 to one decimal place 3.5)

Loren replied on : 15 of 15


I suspect it’s never been added because it’s not too hard to write the code, and I don’t think very many people have requested it. If you would like to see a version shipping with MATLAB, use the link on the right of my blog to contact technical support and put in your plea for an enhancement.

Finally, you can check out the File Exchange. When I searched for rounding or rounding significant, I found quite a few possible solutions (with some other stuff also mixed in.