sprintf for 3.1901 to show 03.190

조회 수: 2 (최근 30일)
Purnendu Nath
Purnendu Nath 2020년 4월 15일
댓글: Purnendu Nath 2020년 4월 15일
I have the numbers
s = 3.1901
and
ss = 13.1905
and want to use
sprintf(formatSpec, s) and sprintf(formatSpec, ss)
to return
03.190 {i.e. rounded to 3DP and the leading zero shown}
and
13.191 {i.e. rounded to 3DP}
so both 3.1901 and 13.1905 should return
2 places before the DP (leading zero for 3.1901)
and
3 places after the DP (rounded to 3 DP)
I am struggling to figure out the correct formatSpec when calling sprintf(formatSpec, ....)

채택된 답변

Walter Roberson
Walter Roberson 2020년 4월 15일
%06.3f should do it.
  댓글 수: 1
Purnendu Nath
Purnendu Nath 2020년 4월 15일
Thanks Walter!
I figured it out after struggling for an hour and then looking on Stack overflow in general printf rather than Matlab specific pages... Your confirmation is of value too, I appreciate it.
Cheers!
Purnendu

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Whos에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by