funstein Posted August 15, 2011 Share Posted August 15, 2011 Hello there, I have a script that has two variables called oc_id and oc_pos. These variables, oc_pos and oc_id, are summoned from a database based on user input. Sometimes it is 120, sometimes 50, sometimes 5. So the number of digits varies. Now I need to convert these into an identifier string for some purpose. But I want them to be 3 digits always, for example, 120, 050, 005. How is that possible? Quote Link to comment https://forums.phpfreaks.com/topic/244829-specifying-amount-of-digits/ Share on other sites More sharing options...
trq Posted August 15, 2011 Share Posted August 15, 2011 echo sprintf("[%03s]\n", $oc_id); Quote Link to comment https://forums.phpfreaks.com/topic/244829-specifying-amount-of-digits/#findComment-1257658 Share on other sites More sharing options...
xyph Posted August 15, 2011 Share Posted August 15, 2011 Alternately: str_pad( $number, 3, '0', STR_PAD_LEFT ); Quote Link to comment https://forums.phpfreaks.com/topic/244829-specifying-amount-of-digits/#findComment-1257741 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.