-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathroundn.m
49 lines (38 loc) · 1.23 KB
/
roundn.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
42
43
44
45
46
47
48
49
function [x,msg] = roundn(x,n)
%ROUNDN Rounds input data at specified power of 10
%
% y = ROUNDN(x) rounds the input data x to the nearest hundredth.
%
% y = ROUNDN(x,n) rounds the input data x at the specified power
% of tens position. For example, n = -2 rounds the input data to
% the 10E-2 (hundredths) position.
%
% [y,msg] = ROUNDN(...) returns the text of any error condition
% encountered in the output variable msg.
%
% See also ROUND
% Copyright 1996-1998 by Systems Planning and Analysis, Inc. and The MathWorks, Inc.
% Written by: E. Byrns, E. Brown
% $Revision: 1.7 $ $Date: 1998/08/10 17:48:04 $
msg = []; % Initialize output
if nargin == 0
error('Incorrect number of arguments')
elseif nargin == 1
n = -2;
end
% Test for scalar n
if max(size(n)) ~= 1
msg = 'Scalar accuracy required';
if nargout < 2; error(msg); end
return
elseif ~isreal(n)
warning('Imaginary part of complex N argument ignored')
n = real(n);
end
% Compute the exponential factors for rounding at specified
% power of 10. Ensure that n is an integer.
factors = 10 ^ (fix(-n));
% Set the significant digits for the input data
x = round(x * factors) / factors;
% Example: frank=roundn(6.12345, -3)
% frank = 6.1230