Search code examples
cformatnumber-formatting

Does a floating point format's width option exclude the decimal point when it's the last element?


I'm reading this document about the secret of printf in order to write my own printf and I found the 2nd ("%5.0f.", e) with e = 2.718281828 printf floating point format and what it produces confusing, why is there four spaces before the 3 and the decimal point?

I thought the output would be 3. with three spaces before the number 3 as the decimal point takes one space itself.


Solution

  • In the format string "%5.0f." there are two parts: a floating point specifier %5.0f and a literal .. The two are not attached in any way.

    The first part prints 4 spaces followed by the number 2, and the second part just prints . independently of the first part.