-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathround2.m
41 lines (39 loc) · 812 Bytes
/
round2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
function z = round2(x,y)
%ROUND2 rounds number to nearest multiple of arbitrary precision.
% Z = ROUND2(X,Y) rounds X to nearest multiple of Y.
%
%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
%
% See also ROUND.
%% defensive programming
error(nargchk(2,2,nargin))
error(nargoutchk(0,1,nargout))
if numel(y)>1
error('Y must be scalar')
end
%%
z = round(x/y)*y;