turn 0 to 0.000

조회 수: 13 (최근 30일)
Chris E.
Chris E. 2012년 11월 26일
Well this is a formatting issue, how can I force any number to have a specified format? Example:
I have 1.23, I need 1.230
I have 0, I need 0.000
I have 0.1011, I need 0.101
I have 5.1019, I need 5.102
I have 20.002, I need 20.002 (stays the same)
I seem to have tried a few methods I found on-line, however still have not found one that works exactly this way. I would never exceed 99.999 or -99.999. Thanks, Chris
  댓글 수: 1
Matt Fig
Matt Fig 2012년 11월 27일
Are you talking about the value of the number or the display of the number?
fprintf('%.17f\n',.1) % Shows all digits

댓글을 달려면 로그인하십시오.

채택된 답변

Image Analyst
Image Analyst 2012년 11월 26일
Use sprintf():
sprintf('%.3f', 0) % to command window.
outputString = sprintf('%.3f', 5.1019) % Sticks it in a string.
  댓글 수: 1
Chris E.
Chris E. 2012년 11월 27일
Thanks this works great!

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

Azzi Abdelmalek
Azzi Abdelmalek 2012년 11월 27일
편집: Azzi Abdelmalek 2012년 11월 27일
use fprintf
a=0;
fprintf('%5.3f\n',a)

Matt Fig
Matt Fig 2012년 11월 27일
You cannot really change the underlying value too much. But as far as a display:
A = [1.23,0,0.1011,5.1019,20.002];
fprintf('\t%.3f\n',A)

카테고리

Help CenterFile Exchange에서 Data Import and Analysis에 대해 자세히 알아보기

제품

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by