Re: Zero Filling Integer

From: Chris Hope (blackhole_at_electrictoolbox.com)
Date: 12/21/04


Date: Wed, 22 Dec 2004 08:38:36 +1300

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/


Relevant Pages

  • Re: Massive HTML coding errors
    ... HTML Dom http://www.w3schools.com/htmldom/dom_reference.asp ' ... JUST BECAUSE YOU SAW SOME SOURCE CODE ... browser and click on "SOURCE". ... libraries might charge a small fee but most are free. ...
    (comp.infosystems.www.authoring.html)
  • Re: Problem with ASP.NET website. Does not display anything but a
    ... between getting a blank page and having no HTML sent to the client. ... Also, HTML errors NEVER produce error messages in a browser, so I would ... view the source code of that page in the browser. ... I am working on a website and suddenly this afternoon i cannot view ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Dealing with PHP Files
    ... language which is interpreted at the server when the page is requested. ... uploaded to the author's server to generate the html page. ... The OP doesn't have anything to do with the source code. ... Unfortunately the only name the browser has is the one in the URL, ...
    (comp.sys.mac.apps)
  • Re: Zero Filling Integer
    ... > Glenn wrote: ... >>Are additional spaces somehow getting surpressed via the HTML? ... > If you look at the source code of the generated page you ... > a table with the cell align property set eg: ...
    (comp.lang.php)
  • Re: Tab to end of previous line perl print
    ... writing a CGI script, and that your output was being viewed through a ... View the source code of your HTML output. ... go to the next line of your browser, you need to print a line-breaking ...
    (perl.beginners)