Page 1 of 1
Add "0" to the front of an integer
Posted: Sat Jan 31, 2009 12:17 pm
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
Re: Add "0" to the front of an integer
Posted: Sat Jan 31, 2009 12:54 pm
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!!
Re: Add "0" to the front of an integer
Posted: Sat Jan 31, 2009 1:17 pm
by Christopher
Re: Add "0" to the front of an integer
Posted: Sat Jan 31, 2009 1:38 pm
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.
Re: Add "0" to the front of an integer
Posted: Sat Jan 31, 2009 2:34 pm
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 ]] )
Re: Add "0" to the front of an integer
Posted: Sat Jan 31, 2009 2:35 pm
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.
Re: Add "0" to the front of an integer
Posted: Sat Jan 31, 2009 3:20 pm
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.