php: turn 1 into 001?

  • jannin
  • Beginner
  • Beginner
  • User avatar
  • Posts: 40
  • Loc: Cranbrook, BC Canada

Post 3+ Months Ago

I'm looking for a way to turn a one- or two-digit number into three digits, just putting some nifty zeros at the beginning (001 instead of 1, 017 instead of 17...). Any way to force the number of digits?

Thanks in advance!

/jannin
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

I haven't tested this, and it's probably not the best method. It's the first thing I thought of:

PHP Code: [ Select ]
 
$output = '';
 
for ($i = 1; $i++; $i < 3)
 
{
 
if ($i < strlen($number))
 
{
 
    $output .= '0';
 
}
 
}
 
$output .= $number;
 
 
  1.  
  2. $output = '';
  3.  
  4. for ($i = 1; $i++; $i < 3)
  5.  
  6. {
  7.  
  8. if ($i < strlen($number))
  9.  
  10. {
  11.  
  12.     $output .= '0';
  13.  
  14. }
  15.  
  16. }
  17.  
  18. $output .= $number;
  19.  
  20.  
  • rjstephens
  • Professor
  • Professor
  • User avatar
  • Posts: 774
  • Loc: Brisbane, Australia

Post 3+ Months Ago

here's another idea. This method is probably better

PHP Code: [ Select ]
 
if ($number < 10)
 
{$output = '00'.$number;}
 
elseif ($number < 100)
 
{$output = '0'.$number;}
 
else
 
{$output = $number;}
 
 
  1.  
  2. if ($number < 10)
  3.  
  4. {$output = '00'.$number;}
  5.  
  6. elseif ($number < 100)
  7.  
  8. {$output = '0'.$number;}
  9.  
  10. else
  11.  
  12. {$output = $number;}
  13.  
  14.  


(notice that you don't need to do an && because elseif's are only executed if none of the previous if's or elseif's were true.
  • jannin
  • Beginner
  • Beginner
  • User avatar
  • Posts: 40
  • Loc: Cranbrook, BC Canada

Post 3+ Months Ago

Thanks rj,
Actually what I ended up doing while waiting for a response here was a sort of mixture of both of your suggestions: I counted how many digits were in the number I had, then added zeros accordingly. Seems strange to me that there would be no function or anything to do it, though.

/jannin
  • RichB
  • Guru
  • Guru
  • User avatar
  • Posts: 1121
  • Loc: Boston

Post 3+ Months Ago

I think sprintf will suit your needs.

PHP Code: [ Select ]
 
<?php
 
$var1 = 1;
 
$var2 = 17;
 
 
 
$var1 = sprintf("%03d", $var1);
 
$var2 = sprintf("%03d", $var2);
 
 
 
echo "var1 = $var1 and var2 = $var2";
 
?>
  1.  
  2. <?php
  3.  
  4. $var1 = 1;
  5.  
  6. $var2 = 17;
  7.  
  8.  
  9.  
  10. $var1 = sprintf("%03d", $var1);
  11.  
  12. $var2 = sprintf("%03d", $var2);
  13.  
  14.  
  15.  
  16. echo "var1 = $var1 and var2 = $var2";
  17.  
  18. ?>

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 134 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.