zero fill numbers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

zero fill numbers

Post by gurjit »

Hi,

I have a number like 9, I want to make it a 6 digit number and represent it like:

000009

If I have any number, how can I zero fill up to 6 numbers?

e.g.

101 would be 000101
1 would be 000001
68 would be 000068
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Code: Select all

str_pad($number, 6, "0", STR_PAD_LEFT);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm old fashioned I guess, I prefer

Code: Select all

sprintf('%06d',$num);
:)
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

worked wonders.... thanks guys
Post Reply