File Exchange

## round2

version 1.2.0.1 (1.39 KB) by Robert Bemis

### Robert Bemis (view profile)

Round input to nearest multiple of arbitrary value.

Updated 01 Sep 2016

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

### Cite As

Robert Bemis (2020). round2 (https://www.mathworks.com/matlabcentral/fileexchange/4261-round2), MATLAB Central File Exchange. Retrieved .

John McDowell

Emil

### Emil (view profile)

FYI.. I get

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

in r2016b

Seyed Resalat

### Seyed Resalat (view profile)

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

Andrew Horchler

### Andrew Horchler (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!

Andrew Horchler

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

Edward Zechmann

### Edward Zechmann (view profile)

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

### Albert Drif (view profile)

it saved me a lot

Humayun Kathuria

### Humayun Kathuria (view profile)

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

MosGeo

Cameron Sparr

JM

Excellent

Afsana

### Afsana (view profile)

thanks , it solved my problem

Rajiv

Very Useful!

David Goldsmith

### David Goldsmith (view profile)

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
%

%% 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

Zhang Yanxiang

Michael

Leo Ka

Evgeny Pr

Husam Aldahiyat

### Husam Aldahiyat (view profile)

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