Extra leading zeros when printing float using printf?

With the %f format specifier, the “2” is treated as the minimum number of characters altogether, not the number of digits before the decimal dot. Thus you have to replace it with 4 to get two leading digits + the decimal point + one decimal digit.

printf("%d:%02d:%04.1f hours\n", 1, 4, 2.123456);

Leave a Comment