chiprivers Posted November 12, 2007 Share Posted November 12, 2007 Is there a simple function that I can use to force 0's (zeros) infront of a given number so that it will always be a fixed number of digits long. ie. If I want to always display a number as 5 digits and I am given the number 50, I want to force it to display as 00050. Quote Link to comment Share on other sites More sharing options...
phpQuestioner Posted November 12, 2007 Share Posted November 12, 2007 you could check the strlen of the number variable and replace zeros as needed based on string length. Quote Link to comment Share on other sites More sharing options...
trq Posted November 12, 2007 Share Posted November 12, 2007 Yes, printf(). Quote Link to comment Share on other sites More sharing options...
rajivgonsalves Posted November 12, 2007 Share Posted November 12, 2007 you can use str_pad function echo str_pad(50,5,"0",STR_PAD_LEFT); more information at http://php.net/str_pad Quote Link to comment Share on other sites More sharing options...
trq Posted November 12, 2007 Share Posted November 12, 2007 Or...as suggested. <?php sprintf("%05d", 50); ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.