[英]How to control the number of exponent digits after 'e' in C printf %e?
I want to control the number of exponent digits after 'e' in C printf %e
?我想控制 C
printf %e
“e”之后的指数位数?
For example, C printf("%e")
result 2.35e+03
, but I want 2.35e+003
, I need 3 digits of exponent, how do I use printf
?例如,C
printf("%e")
结果2.35e+03
,但我想要2.35e+003
,我需要 3 位指数,我该如何使用printf
?
Code:代码:
#include<stdio.h>
int main()
{
double x=34523423.52342353;
printf("%.3g\n%.3e",x,x);
return 0;
}
Result: http://codepad.org/dSLzQIrn结果: http : //codepad.org/dSLzQIrn
3.45e+07
3.452e+07
I want我想要
3.45e+007
3.452e+007
But interestingly, I got the right results in Windows with MinGW.但有趣的是,我在 Windows 中使用 MinGW 得到了正确的结果。
"...The exponent always contains at least two digits, and only as many more digits as necessary to represent the exponent. ..." C11dr §7.21.6.1 8
“...指数始终包含至少两位数字,并且仅包含表示指数所需的更多数字。...” C11dr §7.21.6.1 8
So 3.45e+07
is compliant (what OP does not want) and 3.45e+007
is not compliant (what OP wants).所以
3.45e+07
是合规的(OP 不想要的)而3.45e+007
不合规(OP 想要的)。
As C does not provide a standard way for code to alter the number of exponent digits, code is left to fend for itself.由于 C 没有为代码提供改变指数位数的标准方法,因此代码需要自己解决。
Various compilers support some control.各种编译器支持一些控制。
visual studio _set_output_format
视觉工作室
_set_output_format
For fun, following is DIY code为了好玩,以下是DIY代码
double x = 34523423.52342353;
// - 1 . xxx e - EEEE \0
#define ExpectedSize (1+1+1 +3 +1+1+ 4 + 1)
char buf[ExpectedSize + 10];
snprintf(buf, sizeof buf, "%.3e", x);
char *e = strchr(buf, 'e'); // lucky 'e' not in "Infinity" nor "NaN"
if (e) {
e++;
int expo = atoi(e);
snprintf(e, sizeof buf - (e - buf), "%05d", expo); // 5 more illustrative than 3
}
puts(buf);
3.452e00007
Also see c++ how to get "one digit exponent" with printf另请参阅c++ 如何使用 printf 获得“一位数指数”
printf
Format tags prototype: printf
格式标签原型:
%[flags][width][.precision][length]specifier
The precision
精度
... This gives ... the number of digits to appear after the radix character for a, A, e, E, f, and F conversions ... .
... 这给出了 ... 出现在 a、A、e、E、f 和 F 转换的基数字符之后的位数...。
You are using the conversion and the precision specifier correctly, the difference is with the implementations of the C library function and the environments on the differing systems.您正确使用了转换和精度说明符,区别在于 C 库函数的实现和不同系统上的环境。 The
precision
specifies the number of digits after the '.'
precision
指定'.'
后的位数'.'
(dot, period, etc..). (点、句点等)。 It does not set the number of characters that represent the exponentiation.
它不设置表示幂的字符数。 The facts that it provides
3 digits
on windows is just the way windows specifies the format, not the way the C standard library specifies that printf
will work.它在 windows 上提供
3 digits
的事实只是 windows 指定格式的方式,而不是 C 标准库指定printf
将工作的方式。
It would take comparing how the source implementations differ to see what is relied on for that piece of the format string.需要比较源实现的不同之处,以了解该格式字符串依赖于什么。 (it will probably boil down to some obscure difference in the way the windows v. linux/unix environments/locale/etc. are defined or specified)
(它可能归结为 windows v. linux/unix 环境/语言环境/等的定义或指定方式的一些模糊差异)
char *nexp(double x, int p, int n) // Number with p digits of precision, n digits of exponent.
{
const int NN=12;
static char s[NN][256];//(fvca)
static int i=-1;
int j,e;
i=(++i)%NN; // Index of what s is to be used...
sprintf(s[i],"%.*lE", p,x); // Number...
for(j=0; s[i][j]; j++) if(s[i][j]=='E') break; // Find the 'E'...
if(s[i][j]=='E') // Found!
{
e= atoi(s[i]+j+1);
sprintf(s[i]+j+1, "%+0*d", n+1,e);
return s[i];
}
else return "***";
}
// Best Regards, GGa
// G_G
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.