Jump to content

Archived

This topic is now archived and is closed to further replies.

judeddokoinu

Wondering if there is an easier way

Recommended Posts

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:

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

Share this post


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

Share this post


Link to post
Share on other sites
sprintf or printf. in your case
[code] $new = sprintf("%02d",$old); [/code]

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

number_format and money_format are also useful functions to consider.

Share this post


Link to post
Share on other sites
You might also try [a href=\"http://php.net/strpad\" target=\"_blank\"]strpad[/a]().

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.