I'm trying to find a good way to print leading 0's, such as 01001 for a zipcode. While the number would be stored as 1001, what is a good way to do it?
I thought of using either case statements/if then to figure out how many digits the number is and then convert it to an char array with extra 0's for printing but I can't help but think there may be a way to do this with the printf format syntax that is eluding me.
The correct solution is to store the zip code in the database as a STRING. Despite the fact that it may look like a number, it isn't. It's a code, where each part has meaning.
A number is a thing you do arithmetic on. A zip code is not that.
Here, "05" says "use 5 digits with leading zeros".
If you are on a *NIX Machine:
This will show a manual page, similar to:
Even though the question is for C, this page may be of aid.
printf allows various formatting options.
ex:
You will save yourself a heap of trouble (long term) if you store a zip code as a character string, which it is, rather than a number, which it is not.