Code covered by the BSD License  

Highlights from
round2

4.68182
4.7 | 23 ratings Rate this file 86 Downloads (last 30 days) File Size: 1.39 KB File ID: #4261

round2

by

Robert Bemis (view profile)

 

15 Dec 2003 (Updated )

Round input to nearest multiple of arbitrary value.

| Watch this File

File Information
Description

Everyone knows about the ROUND function for converting floating point values to their nearest whole number or integer value, but have you ever wanted to round off values to something other than whole numbers? This simple utility function does just that.

 Example 1: round PI to 2 decimal places
    >> round2(pi,0.01)
    ans =
          3.14
 
 Example 2: round PI to 4 decimal places
    >> round2(pi,1e-4)
    ans =
          3.1416
 
 Example 3: round PI to 8-bit fraction
    >> round2(pi,2^-8)
    ans =
          3.1406
 
 Examples 4-6: round PI to other multiples
    >> round2(pi,0.05)
    ans =
          3.15
    >> round2(pi,2)
    ans =
          4
    >> round2(pi,5)
    ans =
          5

Acknowledgements

This file inspired Round2 Dp, Graph Digitzer, Round To Specified Digits Place, Round To A Specified Number Of Significant Digits, and Roundfrac.

MATLAB release MATLAB 6.5 (R13)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (28)
13 Jan 2015 John D'Errico

John D'Errico (view profile)

Note that this function is no longer needed in recent versions of MATLAB, which now allows the same functionality, and more in round itself. No toolbox is needed.

Comment only
12 Jan 2015 Abhinav Gaur  
26 Jul 2013 Andrew

Andrew (view profile)

This function should not be used except with the understanding that it is subject to errors at certain values (e.g., as @Will pointed out) due to the nature of floating-point. It's also not even vectorized!

26 Jul 2013 Andrew

Andrew (view profile)

 
13 May 2013 Will

Will (view profile)

Does not work in all cases. Try rounding 3.55 to 1 decimal place and you get 3.5 instead of 3.6

Comment only
28 Nov 2012 Gulsad

Gulsad (view profile)

 
18 Apr 2012 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).

Comment only
10 Apr 2012 Albert Drif

it saved me a lot

06 Apr 2012 Humayun Kathuria

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

02 Apr 2012 Mustafa  
09 Nov 2011 Cameron Sparr  
19 Jul 2011 JM

JM (view profile)

Excellent

25 Nov 2010 Afsana

Afsana (view profile)

thanks , it solved my problem

Comment only
10 Oct 2010 Rajiv

Rajiv (view profile)

Very Useful!

14 Sep 2010 David Goldsmith

I've added keyword-activated floor, ceil, and fix functionality to this; I don't know how to submit an enhancement to a file acquired here, so I'm just pasting the source below (BSD Lic.)

function z = round2(x,y,swtch)
%ROUND2 rounds number to nearest multiple of arbitrary precision.
% Z = ROUND2(X,Y) rounds X to nearest multiple of Y.
% swtch = 'floor' does the same thing, only towards -inf
% = 'ceil' same, only towards +inf
% = 'fix' same, only towards 0
%
%Example 1: round PI to 2 decimal places
% >> round2(pi,0.01)
% ans =
% 3.14
%
%Example 2: round PI to 4 decimal places
% >> round2(pi,1e-4)
% ans =
% 3.1416
%
%Example 3: round PI to 8-bit fraction
% >> round2(pi,2^-8)
% ans =
% 3.1406
%
%Examples 4-6: round PI to other multiples
% >> round2(pi,0.05)
% ans =
% 3.15
% >> round2(pi,2)
% ans =
% 4
% >> round2(pi,5)
% ans =
% 5
%Examples 7-10: round, floor, ceil, and fix PI to nearest 1/7th
%round2(pi,1/7) % recall that PI ~= 3+1/7...
% ans =
% 3.1429
% round2(pi,1/7,'floor') % ... but is < 3+1/7
% ans =
% 3
% round2(pi,1/7,'ceil')
% ans =
% 3.1429
% round2(pi,1/7,'fix')
% ans =
% 3
%
% See also ROUND, FLOOR, CEIL, FIX.

%% defensive programming
error(nargchk(2,3,nargin))
error(nargoutchk(0,1,nargout))
if numel(y)>1
error('Y must be scalar')
end

%%
if nargin < 3
swtch = '';
end
switch swtch
case 'floor'
z = floor(x/y)*y;
case 'ceil'
z = ceil(x/y)*y;
case 'fix'
z = fix(x/y)*y;
otherwise
z = round(x/y)*y;
end

Comment only
07 Sep 2010 Jon

Jon (view profile)

A great simple solution

16 Jun 2010 Zhang Yanxiang  
03 Jun 2010 Michael  
28 May 2010 Leo Ka

Leo Ka (view profile)

 
25 Dec 2009 Evgeny Pr

Evgeny Pr (view profile)

 
10 Feb 2009 Husam Aldahiyat  
13 May 2008 A Anand

very useful

27 Mar 2008 Niki M

Mate you're a lifesaver I have been looking for something like this for a while.

30 Sep 2007 Amer Zak

Simple yet very useful!

05 Sep 2007 Asan G  
27 Aug 2007 Kai M  
04 Jun 2007 j lee  
21 Sep 2006 Seema Sangari  
Updates
12 Jan 2010

copyright

07 Jun 2010

Minor bug fix (error message typo)

Contact us