Code covered by the BSD License  

Highlights from
round2

5.0

5.0 | 16 ratings Rate this file 188 Downloads (last 30 days) File Size: 1.39 KB File ID: #4261

round2

by Robert Bemis

 

15 Dec 2003 (Updated 07 Jun 2010)

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 submission has inspired the following:
Round to a Specified Number of Significant Digits, Round to Specified Digits Place, Graph Digitzer
MATLAB release MATLAB 6.5 (R13)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (18)
21 Sep 2006 Seema Sangari  
04 Jun 2007 j lee  
27 Aug 2007 Kai M  
05 Sep 2007 Asan G  
30 Sep 2007 Amer Zak

Simple yet very useful!

27 Mar 2008 Niki M

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

13 May 2008 A Anand

very useful

10 Feb 2009 Husam Aldahiyat  
25 Dec 2009 Evgeny Pr  
28 May 2010 Leo Ka  
03 Jun 2010 Michael  
16 Jun 2010 Zhang Yanxiang  
07 Sep 2010 Jon

A great simple solution

14 Sep 2010 David

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

10 Oct 2010 Rajiv

Very Useful!

25 Nov 2010 Afsana

thanks , it solved my problem

19 Jul 2011 JM

Excellent

09 Nov 2011 Cameron Sparr  
Please login to add a comment or rating.
Updates
29 Jun 2006

Expanded help text with examples, including binary fraction (analagous to fixed point math).

12 Jan 2010

copyright

07 Jun 2010

Minor bug fix (error message typo)

Tag Activity for this File
Tag Applied By Date/Time
round function Robert Bemis 22 Oct 2008 07:11:06
point values Robert Bemis 22 Oct 2008 07:11:06
whole number Robert Bemis 22 Oct 2008 07:11:06
integers Robert Bemis 22 Oct 2008 07:11:06
values Robert Bemis 22 Oct 2008 07:11:06
rounding Robert Bemis 22 Oct 2008 07:11:06

Contact us at files@mathworks.com