Quantcast
Channel: Printing leading 0's in C - Stack Overflow
Browsing latest articles
Browse All 12 View Live

Answer by Brad Jennings for Printing leading 0's in C?

If you need to store the zipcode in a character array zipcode[] , you can use this: snprintf( zipcode, 6, "%05.5d", atoi(zipcode));

View Article



Answer by rch for Printing leading 0's in C?

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, "%...

View Article

Answer by pro3carp3 for Printing leading 0's in C?

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.

View Article

Answer by JeeBee for Printing leading 0's in C?

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...

View Article

Answer by James Curran for Printing leading 0's in C?

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...

View Article


Answer by Dan Hewett for Printing leading 0's in C?

sprintf(mystring, "%05d", myInt); Here, "05" says "use 5 digits with leading zeros".

View Article

Answer by Paul Tomblin for Printing leading 0's in C?

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...

View Article

Answer by Adam Bellaire for Printing leading 0's in C?

You place a zero before the minimum field width: printf("%05d",zipcode);

View Article


Answer by Trent for Printing leading 0's in C?

printf allows various formatting options. ex: printf("leading zeros %05d", 123);

View Article


Answer by EvilTeach for Printing leading 0's in C?

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...

View Article

Printing leading 0's in C?

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...

View Article

Answer by chqrlie for Printing leading 0's in C

There are 2 ways to output your number with leading zeroes:Using the 0 flag and the width specifier:int zipcode = 123;printf("%05d\n", zipcode); // outputs 00123Using the precision specifier:int...

View Article
Browsing latest articles
Browse All 12 View Live


Latest Images