tags:

views:

1997

answers:

10

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.

+28  A: 

printf("%05d", zipCode);

EvilTeach
Wow thats great. Is there a way to pad spaces after a variable as well?
zxcv
Yes, there is. But I'll leave that as an exercise. :D
Vulcan Eager
what do you mean by pad spaces after a variable?
EvilTeach
So it looks like column spacing, where if you name is Bob I'll add 7 spaces after printing Bob, and if your name is Einstein I'd pad spaces. Right now I'm doing it via if checks and printing a temp variable as well.
zxcv
Try searching you compiler documentation (or the internet) for printf formatting.
Bdoserror
man printf, thats a great idea- Been more use to the java version of print, thanks for the advices!
zxcv
Please do not store zipcodes as numbers. Some countries have letters in their zipcode.
Sec
just insert blanks after the %05d, until you get to the column you want
EvilTeach
A: 

Convert it to a string and pad it.

Esteban Araya
+2  A: 

printf allows various formatting options.

ex:

printf("leading zeros %05d", 123);
Trent
+8  A: 

You place a zero before the minimum field width:

printf("%05d",zipcode);
Adam Bellaire
+2  A: 
man 3 printf

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.

Paul Tomblin
A: 

sprintf(mystring, "%05d", myInt);

"05" says use 5 digits, with leading zeros.

Dan Hewett
+35  A: 

The correct solution is : Store the Zipcode 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 zipcode is not that.

James Curran
Ya. Your observation is absolutely correct. That is what I do. However the person asking the question is probably trying to deal with homework, instead of production code. The answer needs to be tailored to the person asking the question.
EvilTeach
I suppose I should have rephrased it more precisely to illustrate I was looking to see how I can do leading and trailing characters in a language I wasn't familiar with. I'll be more careful with arbitrary examples in the future!
zxcv
+4  A: 

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

JeeBee
A: 

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.

pro3carp3
A: 

my answer is ("foe leading zeros %3d",a%3)

Muhammad Farooq