my question is simple. I want to print integers with a specified amount of leading zeros, using printf. However, the number of leading zeros is decided runtime, not known a priori. How could I do that?
If I knew the number of characters (let's say 5), it would be
printf("%05d", number);
But I don't know if it will be 5.
You can pass a width using *
:
printf("%0*d", 5, number);
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