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. Link to comment https://forums.phpfreaks.com/topic/76959-number-format/ 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. Link to comment https://forums.phpfreaks.com/topic/76959-number-format/#findComment-389694 Share on other sites More sharing options...
trq Posted November 12, 2007 Share Posted November 12, 2007 Yes, printf(). Link to comment https://forums.phpfreaks.com/topic/76959-number-format/#findComment-389695 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 Link to comment https://forums.phpfreaks.com/topic/76959-number-format/#findComment-389715 Share on other sites More sharing options...
trq Posted November 12, 2007 Share Posted November 12, 2007 Or...as suggested. <?php sprintf("%05d", 50); ?> Link to comment https://forums.phpfreaks.com/topic/76959-number-format/#findComment-389719 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.