www.webdeveloper.com
Results 1 to 13 of 13

Thread: easy question..can not find answer

  1. #1
    Join Date
    Jun 2006
    Posts
    204

    easy question..can not find answer

    I have a number 1 through 9 but I need to make it a 2 digit number so it reads 01 or 05 or 07 . What is the code to do this?

  2. #2
    Join Date
    Oct 2005
    Location
    California
    Posts
    603
    $num=0.$num

  3. #3
    Join Date
    Jun 2006
    Posts
    204

    Has To Be A Better Way!

    There has to be a PHP function that does this... There is no way that everyone uses $num=0.$num

    Let me rephrase what I want... I could have a number anywhere from 1-20


    I want the number to be two digits no matter what

  4. #4
    Join Date
    Oct 2005
    Location
    West Chester, PA
    Posts
    244
    you can use the lpad function:
    PHP Code:
    $num lpad($num20); 
    this will pad the left side with a zero if the length is less than 2.
    Woohoo! It wor... er, wait, no it doesn't...
    PHP Coding Standard PHP Namespaces

  5. #5
    Join Date
    Oct 2005
    Location
    California
    Posts
    603
    <?php
    $num="";
    for($i=1;$i<21;$i++){
    $num[$i]='0'.$i;
    }
    ?>

  6. #6
    Join Date
    Jun 2006
    Posts
    204
    thank you AARONBDAVIS....but lpad does not seem to work
    Last edited by firman; 07-27-2006 at 08:38 PM.

  7. #7
    Join Date
    Oct 2005
    Location
    California
    Posts
    603
    PHP Code:
    <?php 
    function padLeft($no){
           
    $num="";
           for(
    $i=1;$i<$no+1;$i++){
           
    $num[$i]='0'.$i;
           }
          return 
    $num;
    }

    $num=padLeft(20);
    echo 
    $num[3];

    ?>
    result of $num[3] is 03
    Last edited by sitehatchery; 07-27-2006 at 08:46 PM.

  8. #8
    Join Date
    Jun 2006
    Posts
    204
    I can not believe that there is not a PHP fuction that makes an interger to be two numbers big

  9. #9
    Join Date
    Oct 2005
    Location
    California
    Posts
    603
    I just wrote one. You can use that.

  10. #10
    Join Date
    Mar 2006
    Posts
    413
    should be a conditional to check whether the number is less than 10 (single digit) i expect you won't want to pad out other numbers.

  11. #11
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,176
    PHP Code:
    $twoDigitNumber sprintf("%02d"$number);
    // or to just output it as such:
    printf("<p>The number is: %02d</p>\n"$number); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  12. #12
    Join Date
    Jun 2004
    Location
    4846′36″ N 910′48″ E
    Posts
    3,747
    yep it's a matter of formatting output, so you should do that instead of actually modifying the number

  13. #13
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,739
    Quote Originally Posted by firman
    I can not believe that there is not a PHP fuction that makes an interger to be two numbers big
    Well the fact is integers do not have leading zeros. If you want leading zeros you must first convert to a string. The code below demonstrates the difference.
    PHP Code:
    <?php

    echo 02;   //  2

    echo '02'// 02

    ?>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles