By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,062 Members | 1,374 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,062 IT Pros & Developers. It's quick & easy.

Zero Filling Integer

P: n/a
Is there a way in PHP to zero fill an integer to a specified length?
For instance, if I have a two digit number, and I want to zero fill it
to four digits, is there a PHP function I can use? Or do I manually
have to add zeros to the front?

Thanks.

Steve
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Steve wrote:
Is there a way in PHP to zero fill an integer to a specified length? For
instance, if I have a two digit number, and I want to zero fill it to
four digits, is there a PHP function I can use? Or do I manually have
to add zeros to the front?


http://www.php.net/sprintf

-- brion vibber (brion @ pobox.com)
Jul 17 '05 #2

P: n/a
Steve wrote:
Is there a way in PHP to zero fill an integer to a specified length?
For instance, if I have a two digit number, and I want to zero fill it
to four digits, is there a PHP function I can use? Or do I manually
have to add zeros to the front?


printf('%04d', $number);
sprintf('%04d', $number);

http://www.php.net/printf
http://www.php.net/sprintf

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #3

P: n/a
$zero_filled = sprintf('%04d', $your_integer);

See sprintf() http://ca3.php.net/sprintf

Dae

"Steve" <ra***********@hotmail.com> a écrit dans le message de
news:m4********************@comcast.com...
Is there a way in PHP to zero fill an integer to a specified length?
For instance, if I have a two digit number, and I want to zero fill it
to four digits, is there a PHP function I can use? Or do I manually
have to add zeros to the front?

Thanks.

Steve

Jul 17 '05 #4

P: n/a
Brion Vibber wrote:
Steve wrote:
Is there a way in PHP to zero fill an integer to a specified length?
For instance, if I have a two digit number, and I want to zero fill it
to four digits, is there a PHP function I can use? Or do I manually
have to add zeros to the front?

http://www.php.net/sprintf

-- brion vibber (brion @ pobox.com)


Examples on that web page are given for justification of text as well:

printf("[%s]\n", $s); // standard string output
printf("[%10s]\n", $s); // right-justification with spaces
printf("[%-10s]\n", $s); // left-justification with spaces

is to output:

[monkey]
[ monkey]
[monkey ]

I tried this, but doesn't seem to be working.. anyone know why?

printf("date [%-20s]", $vdat3);

this yields the following:

date ['2004-12-21' ]

Are additional spaces somehow getting surpressed via the HTML?

Jul 17 '05 #5

P: n/a
Glenn wrote:
Brion Vibber wrote:
Steve wrote:
Is there a way in PHP to zero fill an integer to a specified length?
For instance, if I have a two digit number, and I want to zero fill
it
to four digits, is there a PHP function I can use? Or do I manually
have to add zeros to the front?

http://www.php.net/sprintf

-- brion vibber (brion @ pobox.com)


Examples on that web page are given for justification of text as well:

printf("[%s]\n", $s); // standard string output
printf("[%10s]\n", $s); // right-justification with spaces
printf("[%-10s]\n", $s); // left-justification with spaces

is to output:

[monkey]
[ monkey]
[monkey ]

I tried this, but doesn't seem to be working.. anyone know why?

printf("date [%-20s]", $vdat3);

this yields the following:

date ['2004-12-21' ]

Are additional spaces somehow getting surpressed via the HTML?


Exactly. Additional spaces are ignored in HTML when it is parsed and
displayed in the browser. Otherwise any spaces used for formatting your
code would appear in the browser and it would be an almighty mess.

If you look at the source code of the generated page (View|Source) you
should see the spaces there.

If you are wanting to right align columns of numbers you are best to use
a table with the cell align property set eg:

<table>
<tr>
<td align="right">1.23</td>
<td align="right">543.77</td>
<td align="right">23.45</td>
<td align="right">8756.00</td>
</tr>
</table>

--
Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/
Jul 17 '05 #6

P: n/a

Glenn wrote (in part):
I tried this, but doesn't seem to be working.. anyone know why?

printf("date [%-20s]", $vdat3);

this yields the following:

date ['2004-12-21' ]

Are additional spaces somehow getting surpressed via the HTML?


Yes, HTML always compresses multiple spaces to one unless you use
"&nbsp;" as your space character.

So

echo str_replace(' ','&nbsp;',sprintf("date [%-20s]",$vdat3));
should produce the output you want.

Ken

Jul 17 '05 #7

P: n/a
Chris Hope wrote:
Glenn wrote:

Brion Vibber wrote:

Steve wrote:
Is there a way in PHP to zero fill an integer to a specified length?
For instance, if I have a two digit number, and I want to zero fill
it
to four digits, is there a PHP function I can use? Or do I manually
have to add zeros to the front?
http://www.php.net/sprintf

-- brion vibber (brion @ pobox.com)


Examples on that web page are given for justification of text as well:

printf("[%s]\n", $s); // standard string output
printf("[%10s]\n", $s); // right-justification with spaces
printf("[%-10s]\n", $s); // left-justification with spaces

is to output:

[monkey]
[ monkey]
[monkey ]

I tried this, but doesn't seem to be working.. anyone know why?

printf("date [%-20s]", $vdat3);

this yields the following:

date ['2004-12-21' ]

Are additional spaces somehow getting surpressed via the HTML?

Exactly. Additional spaces are ignored in HTML when it is parsed and
displayed in the browser. Otherwise any spaces used for formatting your
code would appear in the browser and it would be an almighty mess.

If you look at the source code of the generated page (View|Source) you
should see the spaces there.

If you are wanting to right align columns of numbers you are best to use
a table with the cell align property set eg:

<table>
<tr>
<td align="right">1.23</td>
<td align="right">543.77</td>
<td align="right">23.45</td>
<td align="right">8756.00</td>
</tr>
</table>

Yeah, using a table is what I ended up doing.. I guess I was being
hopefull that, if php provided the fillers, that it would output that way.

I'll try the str_replace that Ken gives in his response.

Thx for the re tho.. I was wondering how to do the alignments within the
cells :)

~Glenn
Jul 17 '05 #8

P: n/a
Ken Robinson wrote:
Glenn wrote (in part):
I tried this, but doesn't seem to be working.. anyone know why?

printf("date [%-20s]", $vdat3);

this yields the following:

date ['2004-12-21' ]

Are additional spaces somehow getting surpressed via the HTML?

Yes, HTML always compresses multiple spaces to one unless you use
"&nbsp;" as your space character.

So

echo str_replace(' ','&nbsp;',sprintf("date [%-20s]",$vdat3));
should produce the output you want.

Ken

Interesting.. I tried the str_replace within the printf function, and it
converted it back to spaces, LOL. I checked the source, and indeed the
spaces are there, but they _are_spaces, not the '&nbsp;' char.

I replaced the printf function with the echo given above, and voila, it
works.

Thanks Ken ;)

~Glenn
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.