Zero Filling Integer

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve

    Zero Filling Integer

    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

    #2
    Re: Zero Filling Integer

    Steve wrote:[color=blue]
    > 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?[/color]



    -- brion vibber (brion @ pobox.com)

    Comment

    • Chris Hope

      #3
      Re: Zero Filling Integer

      Steve wrote:
      [color=blue]
      > 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?[/color]

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




      --
      Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

      Comment

      • Daedalus

        #4
        Re: Zero Filling Integer

        $zero_filled = sprintf('%04d', $your_integer);

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

        Dae

        "Steve" <racquetballer@ hotmail.com> a écrit dans le message de
        news:m4SdnZh08d AHv1vcRVn-gw@comcast.com. ..[color=blue]
        > 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[/color]


        Comment

        • Glenn

          #5
          Re: Zero Filling Integer

          Brion Vibber wrote:
          [color=blue]
          > Steve wrote:
          >[color=green]
          >> 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?[/color]
          >
          >
          > http://www.php.net/sprintf
          >
          > -- brion vibber (brion @ pobox.com)[/color]

          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?

          Comment

          • Chris Hope

            #6
            Re: Zero Filling Integer

            Glenn wrote:
            [color=blue]
            > Brion Vibber wrote:
            >[color=green]
            >> Steve wrote:
            >>[color=darkred]
            >>> 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?[/color]
            >>
            >>
            >> http://www.php.net/sprintf
            >>
            >> -- brion vibber (brion @ pobox.com)[/color]
            >
            > 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?[/color]

            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">5 43.77</td>
            <td align="right">2 3.45</td>
            <td align="right">8 756.00</td>
            </tr>
            </table>

            --
            Chris Hope - The Electric Toolbox - http://www.electrictoolbox.com/

            Comment

            • Ken Robinson

              #7
              Re: Zero Filling Integer


              Glenn wrote (in part):[color=blue]
              > 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?[/color]

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

              So

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

              Ken

              Comment

              • Glenn

                #8
                Re: Zero Filling Integer

                Chris Hope wrote:[color=blue]
                > Glenn wrote:
                >
                >[color=green]
                >>Brion Vibber wrote:
                >>
                >>[color=darkred]
                >>>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)[/color]
                >>
                >>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("dat e [%-20s]", $vdat3);
                >>
                >>this yields the following:
                >>
                >>date ['2004-12-21' ]
                >>
                >>Are additional spaces somehow getting surpressed via the HTML?[/color]
                >
                >
                > 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">5 43.77</td>
                > <td align="right">2 3.45</td>
                > <td align="right">8 756.00</td>
                > </tr>
                > </table>
                >[/color]
                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

                Comment

                • Glenn

                  #9
                  Re: Zero Filling Integer

                  Ken Robinson wrote:
                  [color=blue]
                  > Glenn wrote (in part):
                  >[color=green]
                  >>I tried this, but doesn't seem to be working.. anyone know why?
                  >>
                  >>printf("dat e [%-20s]", $vdat3);
                  >>
                  >>this yields the following:
                  >>
                  >>date ['2004-12-21' ]
                  >>
                  >>Are additional spaces somehow getting surpressed via the HTML?[/color]
                  >
                  >
                  > Yes, HTML always compresses multiple spaces to one unless you use
                  > "&nbsp;" as your space character.
                  >
                  > So
                  >
                  > echo str_replace(' ','&nbsp;',spri ntf("date [%-20s]",$vdat3));
                  > should produce the output you want.
                  >
                  > Ken
                  >[/color]
                  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

                  Comment

                  Working...