File Exchange

image thumbnail

ROUND2

version 1.0 (1.65 KB) by

Round to a specified number of decimals.

4.33333
12 Ratings

0 Downloads

Updated

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.

  See also: ROUND, FIX, FLOOR, CEIL, FIX2.

Comments and Ratings (20)

Peter Kabal

Peter Kabal (view profile)

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
Syntax

roundn(x,n)
Description

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.

Nikolaos

thanks

Thank you Mukhtar. You saved my day.

Daniel

Daniel (view profile)

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.

Updates

another hidden bug fixed!

major bug fixed!

improved version

MATLAB Release
MATLAB 7 (R14)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video