简体   繁体   English

如何控制C printf %e 中'e' 后的指数位数?

[英]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.

相关问题 斐波那契函数在C中具有大数字(即1000个数字) - Fibonacci function with big number (i.e. 1000 digits) in C 用于计算 c 中 e 位数的应用程序 - App for computing digits of e in c printf或iostream如何指定该点后的最大位数 - How does printf or iostream specify maximum number of digits after the point C.如何根据数字的大小从数字中获取前N个数字(例如,数字= 1234 n = 2) - C. How to get the first N digits from a number depending on it's size (e.g. number = 1234 n = 2) 如何翻转双精度指数(例如1e300-&gt; 1e-300)? - How to flip the exponent of a double (e.g. 1e300->1e-300)? C function 以 k 的指数形式打印数字 n,对于 2&lt;=k&lt;=16。 EG for n=200 and k=8, print is “200=3*8**2+1*8**1” - C function that prints the number n in the exponent form of k, for 2<=k<=16. E.G. for n=200 and k=8, the print is “200=3*8**2+1*8**1” 如何限制 printf 在小数点后显示的位数? - How can I limit the number of digits displayed by printf after the decimal point? 如何在C处使用printf命令,使其仅显示特定数目的数字或字符? - How can I use printf command at C so that it appears only a specific number of digits or characters? printf%e科学计数法在C中给出了错误的值 - printf %e scientific notation giving wrong value in C 打印浮点数,使指数标记为“* 10 ^”而不是“e” - Printing float such that exponent is marked with “*10^” instead of “e”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM