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 8 5052
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)
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/
$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
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?
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/
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
" " as your space character.
So
echo str_replace(' ',' ',sprintf("date [%-20s]",$vdat3));
should produce the output you want.
Ken
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
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 " " as your space character.
So
echo str_replace(' ',' ',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 ' ' char.
I replaced the printf function with the echo given above, and voila, it
works.
Thanks Ken ;)
~Glenn This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Margaret MacDonald |
last post by:
Probably it's something dumb that I'm doing/not doing, but echo
doesn't seem to want to echo a literal zero when the value of some var
is zero. E.g.:
$foo = 0 ;
echo 'foo is ' . $foo ;
...
|
by: Andrey |
last post by:
What does the standard say about zero-initializing of static
structures/classes, specifically: is it guaranteed by the standard
that the alignment fillers between the members will also be...
|
by: Steve |
last post by:
I have a database where I want to use an autonumber field to make SKUs
for a new product when it is entered. Is there a way I can make the
field zero fill to a certain length? For instance, if I...
|
by: I wish |
last post by:
#include <string.h>
int a;
memset( a, 0, sizeof(a) );
Does that guarantee all bits zero?
--
|
|
by: Mantorok Redgormor |
last post by:
Finally, control is returned to the host environment. If the value of
status is zero or EXIT_SUCCESS, an implementation-defined form of the
status successful termination is returned.
beyond this...
| |
by: 69dbb24b2db3daad932c457cccfd6 |
last post by:
Hello,
I have to initialize all elements of a very big float point array to
zero. It seems memset(a, 0, len) is faster than a simple loop. I just
want to know whether it is safe to do so, since I...
|
by: gk245 |
last post by:
I mean, anything that follows a 0 is automatically turned into a octal
number. I want to have a integer variable that will hold numbers as
integers even if they begin with a zero.
for example:...
|
by: angellian |
last post by:
Sorry to raise a stupid question but I tried many methods which did
work.
how can I conserve the initial zero when I try to convert STR(06) into
string in SQL statment?
It always gives me 6...
|
by: lovecreatesbea... |
last post by:
Do you prefer malloc or calloc?
p = malloc(size);
Which of the following two is right to get same storage same as the
above call?
p = calloc(1, size);
p = calloc(size, 1);
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...
| |