MATLAB: How to round a number to any precision I define

digitshundredMATLABtentenththousandthousandth

I would like to round a number to the nearest hundredth or to any other precision that I define.

Best Answer

The ability to round a number to a custom precision is available in the ROUNDN function, as part of the Mapping Toolbox.
If you do not have the Mapping Toolbox, you may work around this issue as in the following code:
a = rand(1);
n = -2;
ra = round(a*10^(-n))/(10^(-n));
In the code above, 'n' is the desired power of ten that you would like to round to.