C语言里要对输出的结果用科学计数法表示保留三位有效数字应该怎么写啊?

如题所述

sorry.由于没有在计算机旁,没有及时看到你的求助,你问:C语言里要对输出的结果用科学计数法表示保留三位有效数字应该怎么写?
我觉得应该是
printf("%.3e",变量名);
而不是
printf("%3e",变量名);
==================
MSDN 中有关printf打印格式串:
%[flags] [width] [.precision] [{h | l | I64 | L}]type
的描述,其中对[.precision]是这么说的:
The third optional field of the format specification is the precision specification.
(大义:格式规约中第三个选项段是关于小数的规则。)
==================
对于打印e, E类型的数据时,[.precision]选项的作用是:
The precision specifies the number of digits to be printed after the decimal point. The last printed digit is rounded.
(大义:该精度指定了打印小数点后的位数,之后的位数会被四舍五入)
Default precision is 6; if precision is 0 or the period (.) appears without a number following it, no decimal point is printed.
(大义:缺省情况下,该精度为6,如果精度值为0或者小数点后没有紧跟着数字,则不会打印小数部分)
==================
例:
#include <stdio.h>
int main()
{
float b = 100000.55555f;
printf("%3e\n",b);
printf("%.3e\n",b);
return 0;
}
输出结果为:
1.000006e+005
1.000e+005来自:求助得到的回答
温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-07-23
polly@nowthen:~/test$ cat liu.c
#include <stdio.h>

int main()
{
    float result = 31415.92653;

    printf("result = %0.3e\n", result); <------ %0.3e即可

    return 0;
}
polly@nowthen:~/test$ gcc -Wall liu.c -o liu
polly@nowthen:~/test$ ./liu
result = 3.142e+04 <------是否是你想要的结果?其中e+04代表10的4次方。

第2个回答  2014-05-05
printf("%3e",你的变量名);追问

哦!谢啦!就是不确定记错没!所以问问!可否再问你一个关于C语言编程的问题啊?

本回答被提问者采纳
第3个回答  2014-05-05
printf("%.3e",你的变量名);
第4个回答  2014-05-05
printf("%.3e",12.345);
相似回答