Why does sprintf('%0.7g', pi) only print out 6 decimal places?
26 次查看(过去 30 天)
显示 更早的评论
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.
0 个评论
回答(1 个)
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 个评论
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 Center 和 File Exchange 中查找有关 Data Type Conversion 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!