printf – Printing leading 0’s in C?

The Question :

361 people think this question is useful

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 Question Comments :

The Answer 1

596 people think this answer is useful
printf("%05d", zipCode);

The 0 indicates what you are padding with and the 5 shows the length of the integer number. For example if you use "%02d" (Useful for dates) this would only pad zeros for numbers in the ones column ie.(06 instead of 6). Example 2, "%03d" would pad 2 zeros for one number in the ones column and pad 1 zero for a number in the tens column. ie. (number 7 padded to 007 and number 17 padded to 017).

The Answer 2

163 people think this answer is useful

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.

The Answer 3

47 people think this answer is useful

You place a zero before the minimum field width:

printf("%05d",zipcode);

The Answer 4

16 people think this answer is useful

Zipcode is a highly localised field, many countries have characters in their postcodes, e.g., UK, Canada. Therefore in this example you should use a string / varchar field to store it if at any point you would be shipping or getting users/customers/clients/etc from other countries.

However in the general case you should use the recommended answer (printf("%05d", number);).

The Answer 5

16 people think this answer is useful
sprintf(mystring, "%05d", myInt);

Here, “05” says “use 5 digits with leading zeros”.

The Answer 6

16 people think this answer is useful

If you are on a *NIX Machine:

man 3 printf

This will show a manual page, similar to:

0 The value should be zero padded. For d, i, o, u, x, X, a, A, e, E, f, F, g, and G conversions, the converted value is padded on the left with zeros rather than blanks. If the 0 and – flags both appear, the 0 flag is ignored. If a precision is given with a numeric conversion (d, i, o, u, x, and X), the 0 flag is ignored. For other conversions, the behavior is undefined.

Even though the question is for C, this page may be of aid.

The Answer 7

6 people think this answer is useful

printf allows various formatting options.

ex:

printf("leading zeros %05d", 123);

The Answer 8

2 people think this answer is useful

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.

The Answer 9

0 people think this answer is useful

More flexible.. Here’s an example printing rows of right-justified numbers with fixed widths, and space-padding.

//---- Header
std::string getFmt ( int wid, long val )
{  
  char buf[64];
  sprintf ( buf, "% *ld", wid, val );
  return buf;
}
#define FMT (getFmt(8,x).c_str())

//---- Put to use
printf ( "      COUNT     USED     FREE\n" );
printf ( "A: %s %s %s\n", FMT(C[0]), FMT(U[0]), FMT(F[0]) );
printf ( "B: %s %s %s\n", FMT(C[1]), FMT(U[1]), FMT(F[1]) );
printf ( "C: %s %s %s\n", FMT(C[2]), FMT(U[2]), FMT(F[2]) );

//-------- Output
      COUNT     USED     FREE
A:      354   148523     3283
B: 54138259 12392759   200391
C:    91239     3281    61423

The function and macro are designed so the printfs are more readable.

The Answer 10

0 people think this answer is useful

If you need to store the zipcode in a character array zipcode[] , you can use this:

snprintf( zipcode, 6, "%05.5d", atoi(zipcode));

Tags:,

Add a Comment