0

When I use this:

printf("%e", 2000.0);

I get output 2.000000e+003 which is correct but very ugly. I then tried like this:

printf("%.1e", 2000.0);

Which outputs 2.0e+003 which is far better. The question is how to remove the 2 padding zeroes in the exponent so that the output is 2.0e+3. Is there a specific printf specifier for that or do I have to write a function manually that analyzes the floating point number and prints it the way I want (I'd hate to have to do that)?

DarkAtom
  • 2,589
  • 1
  • 11
  • 27
  • Check your system version of `man 3 printf`. The conversion specifier `e, E` may hold this information. In my case, the exposant will always contains at least two digits. – AugustinLopez Sep 15 '19 at 09:37
  • Are you sure the output from the first version was “2.000000e+003”? Per the C standard, “The exponent always contains at least two digits, and only as many more digits as necessary to represent the exponent.” (C 2018 7.21.6.1 8.) – Eric Postpischil Sep 15 '19 at 10:59

1 Answers1

1

The standard C features for printf do not provide any means of controlling the number of digits in the exponent. If you want a different format, you will have to write your own code or obtain code from somewhere else.

(The %a conversion does use only as many digits in the exponent as needed, but it prints the value with hexadecimal.)

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312