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.
See also: ROUND, FIX, FLOOR, CEIL, FIX2.
Mukhtar Ullah (2021). ROUND2 (https://www.mathworks.com/matlabcentral/fileexchange/6077-round2), MATLAB Central File Exchange. Retrieved .
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).
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).
Guys! Use "roundn" if you want to use a simple MATLAB built-in function.
Thank you Mukhtar. You saved my day.
Thanks a lot! Unbelievable Matlab does not have one in their main functions!
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.
works for me!!! great timely help!!!
Thanks a lot
Regarding roundn- I found it in the Mapping toolbox from MATLAB.
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.
Thank you. Great addition for me. :)
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.
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.
Same comments as Johnny C. except for the "||" and "&&" substitution.
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.
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.
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.
Find the treasures in MATLAB Central and discover how the community can help you!Start Hunting!