File Exchange

image thumbnail

round2

version 1.2.0.1 (1.39 KB) by

Round input to nearest multiple of arbitrary value.

15 Downloads

Updated

View License

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

Comments and Ratings (30)

Emil

Emil (view profile)

FYI.. I get

Warning: NARGCHK will be removed in a future release. Use NARGINCHK or NARGOUTCHK instead.

in r2016b

Thank you, good Job!

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.

Abhinav Gaur

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!

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

Gulsad

Gulsad (view profile)

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).

Albert Drif

it saved me a lot

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

Mustafa

Cameron Sparr

Cameron Sparr (view profile)

JM

JM (view profile)

Excellent

Afsana

Afsana (view profile)

thanks , it solved my problem

Rajiv

Rajiv (view profile)

Very Useful!

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

Jon

Jon (view profile)

A great simple solution

Michael

Leo Ka

Leo Ka (view profile)

Evgeny Pr

Evgeny Pr (view profile)

Husam Aldahiyat

A Anand

very useful

Niki M

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

Amer Zak

Simple yet very useful!

Asan G

Kai M

j lee

Seema Sangari

Updates

1.2.0.1

Updated license

1.2

Minor bug fix (error message typo)

1.1

copyright

MATLAB Release
MATLAB 6.5 (R13)

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

» Watch video