Printing leading 0's in C?

前端 未结 10 1410
[愿得一人]
[愿得一人] 2020-11-22 17:26

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

10条回答
  •  庸人自扰
    2020-11-22 18:05

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

提交回复
热议问题