Code covered by the BSD License  

Highlights from
round2

4.66667

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

round2

by

 

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 A Specified Number Of Significant Digits, and Round To Specified Digits Place.

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 (26)
26 Jul 2013 Andrew

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  
13 May 2013 Will

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

28 Nov 2012 Gulsad  
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).

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

Excellent

25 Nov 2010 Afsana

thanks , it solved my problem

10 Oct 2010 Rajiv

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

07 Sep 2010 Jon

A great simple solution

16 Jun 2010 Zhang Yanxiang  
03 Jun 2010 Michael  
28 May 2010 Leo Ka  
25 Dec 2009 Evgeny Pr  
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