Is it possible to control number of digits shown in exponent printed by scientific notation (e
) of printf?
This code
#include <stdio.h>
int main(void) {
printf("%6.3e", 403.0);
return 0;
}
produces (depends on compiler/platform):
4.030e+002
(VS2010
) or 4.030e+02
(gcc 4.3.4
) or even 4.030e+2
The different number of digits in exponents can easily confuse a diff tool when comparing files generated on different platforms.
You cannot do that with C printf()
.
According to C99 standard doc 7.19.6.1 for %e
and %f
: The exponent always contains at least two digits, and only as many more digits as necessary to represent the exponent. If the value is zero, the exponent is zero
You could however, write your own wrapper function, where you calculate the base and exponent separately, to get the output in desired format.
Something like:
char* printexponent(double x)
{
//calculate base here
//calculate exponent here
//create the floating point string and return it
}
Or you could manipulate the output of the printf %e
to create your own string.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With