No BSD License  

Highlights from
ROUND2

4.4

4.4 | 10 ratings Rate this file 22 Downloads (last 30 days) File Size: 1.65 KB File ID: #6077

ROUND2

by Mukhtar Ullah

 

20 Oct 2004 (Updated 14 Apr 2005)

Round to a specified number of decimals.

| Watch this File

File Information
Description

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.

Acknowledgements
This submission has inspired the following:
Round to a Specified Number of Significant Digits, Round to Specified Digits Place
MATLAB release MATLAB 7 (R14)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (16)
13 Apr 2005 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.

19 Jul 2005 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.

02 Aug 2005 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.

11 Sep 2005 Serena F.

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

11 Apr 2006 Kurt Rosenberger  
11 May 2007 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.

05 Feb 2008 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.

18 Feb 2008 Angel Atanasov

Thank you. Great addition for me. :)

27 Apr 2008 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.

30 Apr 2008 John Greeb

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

08 Jun 2008 Kian Chai

Thanks a lot

11 Oct 2008 Srikant Vallabhajosula

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

14 Oct 2008 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.

19 Jul 2010 Daniel

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

14 Aug 2011 Soumyadipta Sarkar

Thank you Mukhtar. You saved my day.

12 Jan 2012 Nikolaos

thanks

Please login to add a comment or rating.
Updates
21 Oct 2004

improved version

14 Apr 2005

major bug fixed!

14 Apr 2005

another hidden bug fixed!

14 Apr 2005

improvement and corrected description

Tag Activity for this File
Tag Applied By Date/Time
round Mukhtar Ullah 22 Oct 2008 07:33:21
decimal places Mukhtar Ullah 22 Oct 2008 07:33:21
utilities Mukhtar Ullah 22 Oct 2008 07:33:21
miscellaneous Mukhtar Ullah 22 Oct 2008 07:33:21
decimal Mukhtar Ullah 22 Oct 2008 07:33:21

Contact us at files@mathworks.com