Why does sprintf('%0.7g', pi) only print out 6 decimal places?

조회 수: 18 (최근 30일)
Lisa Lee
Lisa Lee 2017년 8월 12일
댓글: Walter Roberson 2017년 8월 13일
I don't understand why sprintf('%0.7g', pi) only prints out 6 decimal places, since it is given a "7". Can someone please help me out here?
I tried both sprintf('%0.7f', pi) and sprintf('%0.7e', pi). Bot of them will print out 7 decimal places.

답변 (1개)

Walter Roberson
Walter Roberson 2017년 8월 12일
For %g format, the significant digits start from the first non-zero digit overall. The leading 3 is the first significant figure. If you sprintf('%.7g', 10*pi) you will see that as the number of digits before the decimal place increases, the digits after the decimal place decreases.
  댓글 수: 2
Lisa Lee
Lisa Lee 2017년 8월 12일
Are you saying that , for %e and %f, the precision number limits the decimal places, however, for %g, the precision number limits the amount of significant figures?
Walter Roberson
Walter Roberson 2017년 8월 13일
Yes. This is documented. See https://www.mathworks.com/help/matlab/ref/sprintf.html#inputarg_formatSpec and notice the table entries talk about "digits after the decimal place" or "significant digits"

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

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by