-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnum2sepstr.m
More file actions
87 lines (76 loc) · 2.25 KB
/
num2sepstr.m
File metadata and controls
87 lines (76 loc) · 2.25 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function out = num2sepstr(numin, format, sep)
% NUM2SEPSTR Convert to string with separation at thousands.
%
% out = NUM2SEPSTR(numin,[format],[sep]) formats numin to a string
% according to the specified format ('%f' by default) and adds the
% sepcified thousands seperators (commas by default).
%
% For non-scalar numin, num2sepstr outpts a cell array of the same shape
% as numin where num2sepstr is called on each value in numin.
%
% String length from format, when specified, is applied before commas are
% added; Instead of...
%
% >> num2sepstr(1e6,'% 20.2f') % length = 22
% ans =
% ' 1,000,000.00'
%
% ...try...
%
% >> sprintf('% 20s',num2sepstr(1e6,'%.2f')) % length = 20
% ans =
% ' 1,000,000.00'
%
% See also SPRINTF, NUM2STR
%
% Created by:
% Robert Perrotta
% Thanks to MathWorks community members Stephen Cobeldick and Andreas J.
% for suggesting a faster, cleaner implementation using regexp and
% regexprep.
if nargin < 2
format = ''; % we choose a format below when we know numin is scalar and real
end
if nargin < 3
sep = ',';
end
if numel(numin)>1
out = cell(size(numin));
for ii = 1:numel(numin)
out{ii} = num2sepstr(numin(ii), format, sep);
end
return
end
if ~isreal(numin)
out = sprintf('%s+%si', ...
num2sepstr(real(numin), format, sep), ...
num2sepstr(imag(numin), format, sep));
return
end
autoformat = isempty(format);
if autoformat
if isinteger(numin) || mod(round(numin, 4), 1) == 0
format = '%.0f';
else
format = '%.4f'; % 4 digits is the num2str default
end
end
str = sprintf(format, numin);
if isempty(str)
error('num2sepstr:invalidFormat', ...
'Invalid format (sprintf could not use "%s").', format)
end
out = regexpi(str, '^(\D*\d{0,3})(\d{3})*(\D\d*)?$', 'tokens', 'once');
if numel(out)
out = [out{1}, regexprep(out{2}, '(\d{3})', [sep,'$1']), out{3}];
else
out = str;
end
if autoformat
% Trim trailing zeros after the decimal. (By checking above for numbers
% that look like integers using autoformat, we avoid ever having ONLY
% zeros after the decimal. There will always be at least one nonzero
% digit following the decimal.)
out = regexprep(out, '(\.\d*[1-9])(0*)', '$1');
end
end