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 ?

For example, C printf("%e") result 2.35e+03 , but I want 2.35e+003 , I need 3 exponent digits, how can I use printf ?

The code:

 #include<stdio.h> int main() { double x=34523423.52342353; printf("%.3g\n%.3e",x,x); return 0; } 

Result: http://codepad.org/dSLzQIrn

 3.45e+07 3.452e+07 

I want

 3.45e+007 3.452e+007 

But it is interesting that I got the correct results on Windows with MinGW.

+7
c printf
source share
3 answers

"... The indicator always contains at least two digits and only as many digits as necessary to represent the exhibitor ...." C11dr ยง7.21.6.1 8

So 3.45e+07 is compatible (which the OP does not want), and 3.45e+007 incompatible (what the OP wants).

Since C does not provide a standard way for the code to change the number of digits of the exponent, the code is reserved for itself.

Various compilers support some control.

visual studio _set_output_format

For fun, the following DIY code

  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 ++ on how to get "one exponent" with printf

+6
source share

printf Tag Prototype Protocol:

 %[flags][width][.precision][length]specifier 

accuracy

... This gives ... the number of digits that will be displayed after the radix character for the conversions a, A, e, E, f and F ....

You correctly use the conversion and the precision specifier; the difference is in the implementation of the functions of the C library and the environment in different systems. precision indicates the number of digits after '.' (point, period, etc.). It does not establish the number of characters representing exponentiation. The facts that he provides 3 digits for windows are how windows specify the format, not how the standard C library indicates that printf will work.

You will need to compare how different implementations of the source differ in order to see what this piece of format string relies on. (this probably comes down to some obscure difference in how windows v. linux / unix environment / locale / etc are defined or specified.)

+1
source share
 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 
0
source share

All Articles