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

15 Ansichten (letzte 30 Tage)
Lisa Lee
Lisa Lee am 12 Aug. 2017
Kommentiert: Walter Roberson am 13 Aug. 2017
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.

Antworten (1)

Walter Roberson
Walter Roberson am 12 Aug. 2017
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 Kommentare
Lisa Lee
Lisa Lee am 12 Aug. 2017
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 am 13 Aug. 2017
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"

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Data Type Conversion finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by