How to control the number of exponent digits after 'e' in C printf %e?
"...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
So 3.45e+07
is compliant (what OP does not want) and 3.45e+007
is not compliant (what OP wants).
As C does not provide a standard way for code to alter the number of exponent digits, code is left to fend for itself.
Various compilers support some control.
visual studio _set_output_format
For fun, following is 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++ how to get "one digit exponent" with printf
printf
Format tags prototype:
%[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 ... .
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. The precision
specifies the number of digits after the '.'
(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.
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)