Add "0" to the front of an integer

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Add "0" to the front of an integer

Post by Skoalbasher »

Hey There,

I don't post here a lot, just look around mostly, but I need some help with something. I am pulling the max value of a table where the year is equal to whatever year I pass to it. Now, when formatting the number for the user to see, I'd like to add some 0s on the front.

Example: If the number is 37, add a 0 to the front to look like "037"
for a number like 2 it'd be "002"
for a 3 digit number i'd be just whatever the number is. Like 137 would just be "137"

This is my code, but it's not outputting the 0's in the front. The data is pulled from an "INT" column in mysql.

Code: Select all

 
$tempnum = $row['MAX(rep_autonumber)'];
if($tempnum < 10)
{
    $newnum = "00".$tempnum + 1;
} else if($tempnum < 100) {
    $newnum = "0".$tempnum + 1;
    } else {
            $newnum = $tempnum + 1;
}
I know the if statments are working because I used different numbers to add, like 2 instead of one, but I'm not getting the "0"'s in the output. AGH! I tried to lookup converting ints to strings and whatnot and I can't find what I need. Please help.

Thanks,
Dan
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Add "0" to the front of an integer

Post by Skoalbasher »

I think I found a good temp solution. Since i'm not storing 0's in the db any way.

Code: Select all

            
$tempnum = $row['MAX(rep_autonumber)'] + 1;
 
if($tempnum < 10)
{
    $newnum = "00";
} else if($tempnum < 100) {
    $newnum = "0";
} else { }
 
echo $newnum.$tempnum;
 
This prints it out exactly as I need it, but id' like to have just one variable instead of echo them both together like that.

-------------------------------------------

Ok, I figured it out for real!!

Code: Select all

 
            $tempnum = $row['MAX(rep_autonumber)'] + 1;
 
            if($tempnum < 10)
            {
                $newnum = "00".$tempnum;;
            } else if($tempnum < 100) {
                $newnum = "0".$tempnum;
            } else { $newnum = $tempnum; }
This works!!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Add "0" to the front of an integer

Post by Christopher »

(#10850)
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Add "0" to the front of an integer

Post by Skoalbasher »

Hmm, I don't understand what the heck they are talking about. All the examples don't seem be anything of what i need. They are taking strings and adding stuff to the front or back of them. i need to take an int and add a string to it. The way I have done it up above seems to actually work great. Because the variable $newnum becomes "0" or "00" and then the number I need.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Add "0" to the front of an integer

Post by Benjamin »

What arborint meant was:

Code: Select all

 
$num = str_pad($num, 3, '0', STR_PAD_LEFT);
//str_pad  ( string $input  , int $pad_length  [, string $pad_string=" "  [, int $pad_type=STR_PAD_RIGHT  ]] )
 
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Add "0" to the front of an integer

Post by Christopher »

Skoalbasher wrote:Hmm, I don't understand what the heck they are talking about. All the examples don't seem be anything of what i need. They are taking strings and adding stuff to the front or back of them. i need to take an int and add a string to it. The way I have done it up above seems to actually work great. Because the variable $newnum becomes "0" or "00" and then the number I need.
Yes. PHP converts between numbers and strings automatically for you, so you can do what you want in one line:

Code: Select all

$tempnum = str_pad($tempnum, 3, '0', STR_PAD_LEFT);
// or
$tempnum = sprintf('%03u', $tempnum);
In addition you can have it pad to 3,4,5... digits by just changing one number -- rather than having to add more elseif's.
(#10850)
User avatar
Skoalbasher
Forum Contributor
Posts: 147
Joined: Thu Feb 07, 2008 8:09 pm

Re: Add "0" to the front of an integer

Post by Skoalbasher »

arborint wrote:
Skoalbasher wrote:Hmm, I don't understand what the heck they are talking about. All the examples don't seem be anything of what i need. They are taking strings and adding stuff to the front or back of them. i need to take an int and add a string to it. The way I have done it up above seems to actually work great. Because the variable $newnum becomes "0" or "00" and then the number I need.
Yes. PHP converts between numbers and strings automatically for you, so you can do what you want in one line:

Code: Select all

$tempnum = str_pad($tempnum, 3, '0', STR_PAD_LEFT);
// or
$tempnum = sprintf('%03u', $tempnum);
In addition you can have it pad to 3,4,5... digits by just changing one number -- rather than having to add more elseif's.
OOOOHHH . So using the first method..

Code: Select all

$tempnum = str_pad($tempnum, 3, '0', STR_PAD_LEFT);
You it will add "0" to the front until 3 digits has been reached?

so if I fed it 56, it'd come out 056?
and if I fed it 3 it'd come out 003?
and one more, if I fed it 157, it'd come out.. 157?

Example:

Code: Select all

 
$tempnum = 56;
$tempnum = str_pad($tempnum, 3, '0', STR_PAD_LEFT);
 
// would output 056?
 
I just tried it, thought it would take me longer to be able to. but that works EXACTLY like I want.. Thanks guys for the help. Sometimes the PHP manual overwelms me.. lol.
Post Reply