how to use format spec in fprintf properly

조회 수: 1 (최근 30일)
Itzik Ben Shabat
Itzik Ben Shabat 2013년 11월 16일
댓글: Itzik Ben Shabat 2013년 11월 16일
Hello,
I have read the help documentation but somtehing doesnt work as expected. say i have a variable
x=3.14
and i want to print to the command window the value of x while allowing 3 digits bedfore the point and 3 after. so i expect to get 003.140 i used
fprintf('%03.3f\n',x)
and got
3.140 as output.
what went wrong ? thanks

채택된 답변

Image Analyst
Image Analyst 2013년 11월 16일
The first number is the total field width, INCLUDING the number after the decimal point. It is not the number before, dot, and then the number after. So if you wanted 3 before, a decimal point, and then 3 more, you'd have to do %7.3f.
  댓글 수: 2
Image Analyst
Image Analyst 2013년 11월 16일
Examples:
>> fprintf('%07.3f\n', pi)
003.142
>> fprintf('%7.3f\n', pi)
3.142
Itzik Ben Shabat
Itzik Ben Shabat 2013년 11월 16일
thanks. i guess i got it wrong :)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by