这是一个非常简单的问题。
有没有办法为printf格式化float,使其具有xx SIGNIFICANT小数?
所以我不是说,%5.3f
浮动,但如果我有
float x=0.00001899383
如果我想最多前三个非零小数,我将如何输出0.0000189?
答案 0 :(得分:10)
“%。3g”将尝试以科学或固定格式输出三位有效数字。
at bjg:
该计划
#include <stdio.h>
int main()
{
double a = 123456789e-15;
int i = 0;
for( i=-10; i <= 10; ++i )
{
printf("%.3g\n", a );
a *= 10;
}
return 0;
}
输出
1.23e-07
1.23e-06
1.23e-05
0.000123
0.00123
0.0123
0.123
1.23
12.3
123
1.23e+03
1.23e+04
1.23e+05
1.23e+06
1.23e+07
1.23e+08
1.23e+09
1.23e+10
1.23e+11
1.23e+12
1.23e+13