How to manually adjust the decimal point?
조회 수: 2 (최근 30일)
이전 댓글 표시
I used (5*rand) to generate the random numbers in the range 0 to 5 and got the answers like 4.5693, 3.4211 etc. The default format long and short only can display 5 digits and 15 digits. How to have 7 digits to get the answers like 4.569312, 3.421178 etc? Please help.
댓글 수: 0
채택된 답변
Wayne King
2012년 9월 25일
편집: Wayne King
2012년 9월 25일
x = 5*rand;
fprintf('%1.6f\n',x)
Or if you want to keep it as a string:
y = sprintf('%1.6f\n',x);
Note now: y
gives you what you want, but if you convert it back to a number with
str2num(y)
the formatting will change back.
댓글 수: 0
추가 답변 (1개)
Daniel Shub
2012년 9월 25일
You could also overload display for class double and format short to make it display 7 digits instead of 15. Since double is the default class in MATLAB it seems a little bit buggier to me than when I suggested overloading display for char. Basically create a folder @double and add it to the MATLAB path. inside that folder add the following function called display.m.
function display(x)
if strcmpi(get(0, 'Format'), 'Short')
name = inputname(1);
if isempty(name)
name = 'ans';
end
builtin('disp', sprintf('%s =\n%20.7f\n', name, x));
else
builtin('display', x);
end
end
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!