forked from lawrennd/ndlutil
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnumsf2str.m
49 lines (46 loc) · 1.1 KB
/
numsf2str.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 str = numsf2str(num, sf);
% NUMSF2STR Convert number to a string with a number of significant digits.
% FORMAT
% DESC converts a number to a string with a given number of
% significant digits.
% ARG num : the number to convert.
% ARG sf : the number of significant figures to show in the string.
% RETURN str : the string containing the number with the given
% number of significant digits.
%
% COPYRIGHT : Neil D. Lawrence, 2005
%
% SEEALSO : num2str, fprintf
% NDLUTIL
str = num2str(num, sf);
tail = [];
ePos = find(str == 'e');
if ~isempty(ePos)
tail = str(ePos+1:end);
str = str(1:ePos-1);
else
if length(str)<sf && isempty(find(str == '.'))
str = [str '.'];
end
end
ind = 1;
while ind<=length(str) && (str(ind) == '0' || str(ind) == '.')
ind = ind+1;
end
count = 0;
while(ind <= length(str))
if str(ind) ~= '.'
count = count + 1;
end
ind = ind +1;
end
while count < sf
str = [str '0'];
count = count + 1;
end
if length(tail)>0
tail = num2str(str2num(tail));
tail = ['\times 10^{' tail '}'];
end
str = [str tail];
end