引用一个很有用的网址:http://www.cplusplus.com/reference/cstdio/printf/
%[flags][width][.precision][length]specifier
先从几个test入手
/************test 1**********/
#include<stdio.h>
int main()
{
printf("%10f\n",2.0); //__2.000000 (2个空格)
printf("%10g\n",2.0); //_________2 (9个空格)
printf("%10g\n",2.1); //_______2.1 (7个空格)
printf("%f\n",2222222.10); //2222222.100000
printf("%g\n",2222222.10); //2.22222e+006
printf("%f\n",22.10); //22.100000
printf("%g\n",22.10); //22.1
printf("%.4g\n",234.5); //234.5
printf("%.4f\n",234.5); //234.5000
}
于是,%g 可以理解为科学技术法输出,没有.xf情况下默认保留六位有效数字(否则保留x位),自动省略后导0再输出
/************test 2***********/
#include<stdio.h>
int main()
{ printf("%.f\n",2.5); //3
printf("%.0f\n",2.5);
Printf 里%f,%g的若干使用
最新推荐文章于 2023-07-24 00:32:14 发布