Jump to content


Photo

Wondering if there is an easier way


  • Please log in to reply
3 replies to this topic

#1 judeddokoinu

judeddokoinu
  • Members
  • PipPip
  • Member
  • 19 posts

Posted 16 May 2006 - 08:13 AM

I'm just looking to trim down my code a bit, if possible.

I was wondering if PHP itself had a function that does what this one that I wrote does:

function leadzero($number)
{
        if ($number < 10)
        {
                $new_number = '0' . $number;
                return $new_number;
        }
        else
        {
                return $number;
        }
}


#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 16 May 2006 - 09:00 AM

You might be able to do it with money_format() - But other than that, I don't think there is one.

#3 ryanlwh

ryanlwh
  • Staff Alumni
  • Advanced Member
  • 511 posts

Posted 16 May 2006 - 04:43 PM

sprintf or printf. in your case
$new = sprintf("%02d",$old);

You can do many different kinds of formatting with sprintf or printf.

number_format and money_format are also useful functions to consider.
Please use EDIT * 100...
Please use
or [php] * 1000...

PLEASE READ THE POSTED SOLUTIONS CAREFULLY * 1000000...

#4 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 16 May 2006 - 05:46 PM

You might also try [a href=\"http://php.net/strpad\" target=\"_blank\"]strpad[/a]().




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users