Zero-pad digits in string












106















I need to cast single figures (1 to 9) to (01 to 09). I can think of a way but its big and ugly and cumbersome. I'm sure there must be some concise way. Any Suggestions










share|improve this question

























  • Possible duplicate of Formatting a number with leading zeros in PHP

    – Organic Advocate
    Dec 6 '16 at 15:27
















106















I need to cast single figures (1 to 9) to (01 to 09). I can think of a way but its big and ugly and cumbersome. I'm sure there must be some concise way. Any Suggestions










share|improve this question

























  • Possible duplicate of Formatting a number with leading zeros in PHP

    – Organic Advocate
    Dec 6 '16 at 15:27














106












106








106


18






I need to cast single figures (1 to 9) to (01 to 09). I can think of a way but its big and ugly and cumbersome. I'm sure there must be some concise way. Any Suggestions










share|improve this question
















I need to cast single figures (1 to 9) to (01 to 09). I can think of a way but its big and ugly and cumbersome. I'm sure there must be some concise way. Any Suggestions







php zero-pad






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 29 '08 at 18:05









scunliffe

48.4k19106148




48.4k19106148










asked Nov 27 '08 at 17:55







Giles




















  • Possible duplicate of Formatting a number with leading zeros in PHP

    – Organic Advocate
    Dec 6 '16 at 15:27



















  • Possible duplicate of Formatting a number with leading zeros in PHP

    – Organic Advocate
    Dec 6 '16 at 15:27

















Possible duplicate of Formatting a number with leading zeros in PHP

– Organic Advocate
Dec 6 '16 at 15:27





Possible duplicate of Formatting a number with leading zeros in PHP

– Organic Advocate
Dec 6 '16 at 15:27












3 Answers
3






active

oldest

votes


















193














First of all, your description is misleading. Double is a floating point data type. You presumably want to pad your digits with leading zeros in a string. The following code does that:



$s = sprintf('%02d', $digit);


For more information, refer to the documentation of sprintf.






share|improve this answer


























  • good job. it work

    – winnie damayo
    Aug 25 '17 at 2:16











  • 09 and 08 is not working in PHP7

    – Hiren Bhut
    Feb 1 '18 at 9:50













  • @HirenBhut No, it works.

    – Konrad Rudolph
    Feb 1 '18 at 10:38











  • @KonradRudolph If i have pass as digit value as integer that time given error, If pass as string that time not problem

    – Hiren Bhut
    Feb 1 '18 at 11:45








  • 2





    @HirenBhut Well that’s completely different, and has nothing to do with sprintf. Check the format of integers, in particular the section about octal digits.

    – Konrad Rudolph
    Feb 1 '18 at 12:34





















81














There's also str_pad



<?php
$input = "Alien";
echo str_pad($input, 10); // produces "Alien "
echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
echo str_pad($input, 6 , "___"); // produces "Alien_"
?>





share|improve this answer































    59














    Solution using str_pad:



    str_pad($digit,2,'0',STR_PAD_LEFT);


    Benchmark on php 5.3




    Result str_pad : 0.286863088608



    Result sprintf : 0.234171152115




    Code:



    $start = microtime(true);
    for ($i=0;$i<100000;$i++) {
    str_pad(9,2,'0',STR_PAD_LEFT);
    str_pad(15,2,'0',STR_PAD_LEFT);
    str_pad(100,2,'0',STR_PAD_LEFT);
    }
    $end = microtime(true);
    echo "Result str_pad : ",($end-$start),"n";

    $start = microtime(true);
    for ($i=0;$i<100000;$i++) {
    sprintf("%02d", 9);
    sprintf("%02d", 15);
    sprintf("%02d", 100);
    }
    $end = microtime(true);
    echo "Result sprintf : ",($end-$start),"n";





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f324358%2fzero-pad-digits-in-string%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown
























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      193














      First of all, your description is misleading. Double is a floating point data type. You presumably want to pad your digits with leading zeros in a string. The following code does that:



      $s = sprintf('%02d', $digit);


      For more information, refer to the documentation of sprintf.






      share|improve this answer


























      • good job. it work

        – winnie damayo
        Aug 25 '17 at 2:16











      • 09 and 08 is not working in PHP7

        – Hiren Bhut
        Feb 1 '18 at 9:50













      • @HirenBhut No, it works.

        – Konrad Rudolph
        Feb 1 '18 at 10:38











      • @KonradRudolph If i have pass as digit value as integer that time given error, If pass as string that time not problem

        – Hiren Bhut
        Feb 1 '18 at 11:45








      • 2





        @HirenBhut Well that’s completely different, and has nothing to do with sprintf. Check the format of integers, in particular the section about octal digits.

        – Konrad Rudolph
        Feb 1 '18 at 12:34


















      193














      First of all, your description is misleading. Double is a floating point data type. You presumably want to pad your digits with leading zeros in a string. The following code does that:



      $s = sprintf('%02d', $digit);


      For more information, refer to the documentation of sprintf.






      share|improve this answer


























      • good job. it work

        – winnie damayo
        Aug 25 '17 at 2:16











      • 09 and 08 is not working in PHP7

        – Hiren Bhut
        Feb 1 '18 at 9:50













      • @HirenBhut No, it works.

        – Konrad Rudolph
        Feb 1 '18 at 10:38











      • @KonradRudolph If i have pass as digit value as integer that time given error, If pass as string that time not problem

        – Hiren Bhut
        Feb 1 '18 at 11:45








      • 2





        @HirenBhut Well that’s completely different, and has nothing to do with sprintf. Check the format of integers, in particular the section about octal digits.

        – Konrad Rudolph
        Feb 1 '18 at 12:34
















      193












      193








      193







      First of all, your description is misleading. Double is a floating point data type. You presumably want to pad your digits with leading zeros in a string. The following code does that:



      $s = sprintf('%02d', $digit);


      For more information, refer to the documentation of sprintf.






      share|improve this answer















      First of all, your description is misleading. Double is a floating point data type. You presumably want to pad your digits with leading zeros in a string. The following code does that:



      $s = sprintf('%02d', $digit);


      For more information, refer to the documentation of sprintf.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Apr 2 '16 at 11:24









      Gras Double

      10.4k54743




      10.4k54743










      answered Nov 27 '08 at 17:59









      Konrad RudolphKonrad Rudolph

      401k1017881037




      401k1017881037













      • good job. it work

        – winnie damayo
        Aug 25 '17 at 2:16











      • 09 and 08 is not working in PHP7

        – Hiren Bhut
        Feb 1 '18 at 9:50













      • @HirenBhut No, it works.

        – Konrad Rudolph
        Feb 1 '18 at 10:38











      • @KonradRudolph If i have pass as digit value as integer that time given error, If pass as string that time not problem

        – Hiren Bhut
        Feb 1 '18 at 11:45








      • 2





        @HirenBhut Well that’s completely different, and has nothing to do with sprintf. Check the format of integers, in particular the section about octal digits.

        – Konrad Rudolph
        Feb 1 '18 at 12:34





















      • good job. it work

        – winnie damayo
        Aug 25 '17 at 2:16











      • 09 and 08 is not working in PHP7

        – Hiren Bhut
        Feb 1 '18 at 9:50













      • @HirenBhut No, it works.

        – Konrad Rudolph
        Feb 1 '18 at 10:38











      • @KonradRudolph If i have pass as digit value as integer that time given error, If pass as string that time not problem

        – Hiren Bhut
        Feb 1 '18 at 11:45








      • 2





        @HirenBhut Well that’s completely different, and has nothing to do with sprintf. Check the format of integers, in particular the section about octal digits.

        – Konrad Rudolph
        Feb 1 '18 at 12:34



















      good job. it work

      – winnie damayo
      Aug 25 '17 at 2:16





      good job. it work

      – winnie damayo
      Aug 25 '17 at 2:16













      09 and 08 is not working in PHP7

      – Hiren Bhut
      Feb 1 '18 at 9:50







      09 and 08 is not working in PHP7

      – Hiren Bhut
      Feb 1 '18 at 9:50















      @HirenBhut No, it works.

      – Konrad Rudolph
      Feb 1 '18 at 10:38





      @HirenBhut No, it works.

      – Konrad Rudolph
      Feb 1 '18 at 10:38













      @KonradRudolph If i have pass as digit value as integer that time given error, If pass as string that time not problem

      – Hiren Bhut
      Feb 1 '18 at 11:45







      @KonradRudolph If i have pass as digit value as integer that time given error, If pass as string that time not problem

      – Hiren Bhut
      Feb 1 '18 at 11:45






      2




      2





      @HirenBhut Well that’s completely different, and has nothing to do with sprintf. Check the format of integers, in particular the section about octal digits.

      – Konrad Rudolph
      Feb 1 '18 at 12:34







      @HirenBhut Well that’s completely different, and has nothing to do with sprintf. Check the format of integers, in particular the section about octal digits.

      – Konrad Rudolph
      Feb 1 '18 at 12:34















      81














      There's also str_pad



      <?php
      $input = "Alien";
      echo str_pad($input, 10); // produces "Alien "
      echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
      echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
      echo str_pad($input, 6 , "___"); // produces "Alien_"
      ?>





      share|improve this answer




























        81














        There's also str_pad



        <?php
        $input = "Alien";
        echo str_pad($input, 10); // produces "Alien "
        echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
        echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
        echo str_pad($input, 6 , "___"); // produces "Alien_"
        ?>





        share|improve this answer


























          81












          81








          81







          There's also str_pad



          <?php
          $input = "Alien";
          echo str_pad($input, 10); // produces "Alien "
          echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
          echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
          echo str_pad($input, 6 , "___"); // produces "Alien_"
          ?>





          share|improve this answer













          There's also str_pad



          <?php
          $input = "Alien";
          echo str_pad($input, 10); // produces "Alien "
          echo str_pad($input, 10, "-=", STR_PAD_LEFT); // produces "-=-=-Alien"
          echo str_pad($input, 10, "_", STR_PAD_BOTH); // produces "__Alien___"
          echo str_pad($input, 6 , "___"); // produces "Alien_"
          ?>






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 27 '08 at 18:10









          LeppyR64LeppyR64

          4,28312330




          4,28312330























              59














              Solution using str_pad:



              str_pad($digit,2,'0',STR_PAD_LEFT);


              Benchmark on php 5.3




              Result str_pad : 0.286863088608



              Result sprintf : 0.234171152115




              Code:



              $start = microtime(true);
              for ($i=0;$i<100000;$i++) {
              str_pad(9,2,'0',STR_PAD_LEFT);
              str_pad(15,2,'0',STR_PAD_LEFT);
              str_pad(100,2,'0',STR_PAD_LEFT);
              }
              $end = microtime(true);
              echo "Result str_pad : ",($end-$start),"n";

              $start = microtime(true);
              for ($i=0;$i<100000;$i++) {
              sprintf("%02d", 9);
              sprintf("%02d", 15);
              sprintf("%02d", 100);
              }
              $end = microtime(true);
              echo "Result sprintf : ",($end-$start),"n";





              share|improve this answer






























                59














                Solution using str_pad:



                str_pad($digit,2,'0',STR_PAD_LEFT);


                Benchmark on php 5.3




                Result str_pad : 0.286863088608



                Result sprintf : 0.234171152115




                Code:



                $start = microtime(true);
                for ($i=0;$i<100000;$i++) {
                str_pad(9,2,'0',STR_PAD_LEFT);
                str_pad(15,2,'0',STR_PAD_LEFT);
                str_pad(100,2,'0',STR_PAD_LEFT);
                }
                $end = microtime(true);
                echo "Result str_pad : ",($end-$start),"n";

                $start = microtime(true);
                for ($i=0;$i<100000;$i++) {
                sprintf("%02d", 9);
                sprintf("%02d", 15);
                sprintf("%02d", 100);
                }
                $end = microtime(true);
                echo "Result sprintf : ",($end-$start),"n";





                share|improve this answer




























                  59












                  59








                  59







                  Solution using str_pad:



                  str_pad($digit,2,'0',STR_PAD_LEFT);


                  Benchmark on php 5.3




                  Result str_pad : 0.286863088608



                  Result sprintf : 0.234171152115




                  Code:



                  $start = microtime(true);
                  for ($i=0;$i<100000;$i++) {
                  str_pad(9,2,'0',STR_PAD_LEFT);
                  str_pad(15,2,'0',STR_PAD_LEFT);
                  str_pad(100,2,'0',STR_PAD_LEFT);
                  }
                  $end = microtime(true);
                  echo "Result str_pad : ",($end-$start),"n";

                  $start = microtime(true);
                  for ($i=0;$i<100000;$i++) {
                  sprintf("%02d", 9);
                  sprintf("%02d", 15);
                  sprintf("%02d", 100);
                  }
                  $end = microtime(true);
                  echo "Result sprintf : ",($end-$start),"n";





                  share|improve this answer















                  Solution using str_pad:



                  str_pad($digit,2,'0',STR_PAD_LEFT);


                  Benchmark on php 5.3




                  Result str_pad : 0.286863088608



                  Result sprintf : 0.234171152115




                  Code:



                  $start = microtime(true);
                  for ($i=0;$i<100000;$i++) {
                  str_pad(9,2,'0',STR_PAD_LEFT);
                  str_pad(15,2,'0',STR_PAD_LEFT);
                  str_pad(100,2,'0',STR_PAD_LEFT);
                  }
                  $end = microtime(true);
                  echo "Result str_pad : ",($end-$start),"n";

                  $start = microtime(true);
                  for ($i=0;$i<100000;$i++) {
                  sprintf("%02d", 9);
                  sprintf("%02d", 15);
                  sprintf("%02d", 100);
                  }
                  $end = microtime(true);
                  echo "Result sprintf : ",($end-$start),"n";






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Mar 31 '17 at 7:50









                  Pang

                  6,9651664105




                  6,9651664105










                  answered Jun 15 '11 at 12:39









                  AlexAlex

                  1,24211417




                  1,24211417






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f324358%2fzero-pad-digits-in-string%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      List item for chat from Array inside array React Native

                      Thiostrepton

                      Caerphilly