Search code examples
cprintfdoubledecimalstdio

How to use printf so that zeros are printed with decimals


I'm running a simple calculation in C, for which, sometimes, the result is exactly 0. I then print these results using something like printf("%0.4g", result). If the result is not 0, it does what I want it do, e.g., the output may be 1.796e+04. However, if the result is exactly 0, which it often is, the output will be 0.

My question is, what can I do to print 0.0000, with 4 decimals, even though the number there might be exactly 0?


Solution

  • the statement

    printf("%0.4e\n", res);

    might solve your problem!