File Exchange

image thumbnail


version (758 Bytes) by Mukhtar Ullah
Round to a specified number of decimals.

1 Download

Updated 14 Apr 2005

No License

ROUND2 Round to a specified number of decimals.

ROUND2(A) returns the same as ROUND(A).
ROUND2(A, 'even') returns the same as ROUND(A). Elements with
fractional part equal to 0.5 are rounded to even integers.
ROUND2(A, N) rounds the elements of A to decimals specified in N.
ROUND2(A, N,'even') rounds the elements of A to decimals specified
in N. Elements with 5 as the (N+1)-th digit after decimal are rounded
to even numbers.


Cite As

Mukhtar Ullah (2021). ROUND2 (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)

Peter Kabal

2016-02-19 Round2 does not work properly:
Round2(1.46, 'even') gives 0.
I believe that the round function should be removed from the definition of the tf(r) function (line 56).

Andrew Horchler

Edward Zechmann

Roundn is in the Mapping Toolbox which costs approximately $1,000.00 for a single user license. It is much simpler; however, it has fewer features and costs $1,000.00. The syntax for using roundn is reproduced from the Mathworks website to help anyone who reads this comment thread. Please let me know if a program with the simple syntax of Roundn would be of benefit to the Matlab Central Users Community.

roundn - Round to multiple of 10n


roundn(x,n) rounds each element of x to the nearest multiple of 10n. The variable n must be scalar, and integer-valued. For complex x, the imaginary and real parts are rounded independently. For n = 0, roundn gives the same result as round. That is, roundn(x,0) == round(x).

Humayun Kathuria

Guys! Use "roundn" if you want to use a simple MATLAB built-in function.



Soumyadipta Sarkar

Thank you Mukhtar. You saved my day.


Thanks a lot! Unbelievable Matlab does not have one in their main functions!

Senthilkumaran K

Ohh.. Now I could understand, why I did not get roundn function. I didn't install Mapping tool box in R2007b. ThxJohn Greeb for the info.

Srikant Vallabhajosula

works for me!!! great timely help!!!

Kian Chai

Thanks a lot

John Greeb

Regarding roundn- I found it in the Mapping toolbox from MATLAB.

Amita Deb

This seems very useful. I am surprised that MATLAB doesn't have a built-in function to to rounding-off up to a desired number of decimal places.

Angel Atanasov

Thank you. Great addition for me. :)

Mukhtar Ullah

Senthilkumaran, I could not find roundn in MATLAB R2007b. May be you have mistyped it? It will be very interseting to know if there is a function like that.

Senthilkumaran K

Hi... Do u have any idea about roundn function..
I used it in R14 version but couldn't find it in 2006b. Anyway your code helped to bridge that gap.

Kurt Rosenberger

Serena F.

Same comments as Johnny C. except for the "||" and "&&" substitution.

Mukhtar Ullah

Oliver Lockwood,
Surely, what you said forms the core of this file; but if you look the help documentation, ROUND2 does more than that. Like when you need rounding to even in 0.5 case.

Oliver Lockwood

Surely this can be done far more simply by the following method, e.g. if you want to round variable mynum to 4 d.p.:

mynum = (round(mynum*1e4)) / 1e4 ;

Please let me know your thoughts.

Johnny C.

A good idea, just what I needed. The function probably works as such in R14 which is the intended environment.

However, I'm using R12.1 and I had to replace "tf = @(x) abs(x - fix(x)) == .5" by "tf = inline('abs(x - fix(x)) == .5')", "||" by "|" and "&&" by "&" and comment away the check for non-scalar input (R12.1 doesn't have the function "isscalar") to make it work.

If it worked right away in a previous Matlab release, I'd rate it at 4.

MATLAB Release Compatibility
Created with R14
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!