num2str default format

조회 수: 9 (최근 30일)
Ilya Gurin
Ilya Gurin 2020년 5월 4일
댓글: Stephen23 2020년 5월 4일
I want to print multiple values using fprintf, but with the same number formatting as num2str. Is there a format string that will give the same behavior as num2str?

답변 (2개)

dpb
dpb 2020년 5월 4일
'%.5g'
is default format iirc what num2str does and the way %g counts precision.
  댓글 수: 1
Stephen23
Stephen23 2020년 5월 4일
>> num = intmax('uint32');
>> num2str(num)
ans =
4294967295
>> sprintf('%.5g',num)
ans =
4.295e+09

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


Walter Roberson
Walter Roberson 2020년 5월 4일
no. num2str does a bunch of width calculations and chooses output format based on integer vs floating point
  댓글 수: 1
Ilya Gurin
Ilya Gurin 2020년 5월 4일
Haha, I want to accept both your answers. sprintf(pi, '%.5g') and num2str(pi) are the same. If I scale the input up by 1E10 or 1E100, the results are different, but I think I like the behavior of '%.5g' better.

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

카테고리

Help CenterFile Exchange에서 Characters and Strings에 대해 자세히 알아보기

태그

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by