SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Sep 2007
    Posts
    23
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to make a number to 6 characters?

    Hi all,

    How do i make a integer that I pass down to have 6 characters by adding 0 infront? Any help would be aprreciated. Thanks~

    For example:
    394->000394
    2832->002832
    52->000052

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do this:
    PHP Code:
    $input 1000;
    echo 
    str_pad($input60STR_PAD_LEFT); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Note: Should only be for display, not for internal use.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Note: Should only be for display, not for internal use.
    Yes login_earth, it would be nice to have the field (in case of database table - MySQL) zero fill. Am I right?

    What you mean "not for internal use"?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You could but if you want to do things with that number like math when you convert it to integer the leading zeros are removed.

    Or in some cases PHP thinks the number is octal which has a leading zero (mostly). Bad things happen.
    But this is a rare problem mostly due to bugs in the system.

    Really just best to leave the leading zeros for display only.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ummm! the following code is working something strange:
    Code php:
    $input = "001000";
    echo $input * 2; # Gives 2000
    
    $input = 001000;
    echo $input * 2; # Gives 1024;
    
    $input = 1000;
    echo $input * 2; # Gives 2000

    Is the second one acting as octal?
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Hmm well the octal of 001000 is 330 so cannot be that...hmmm
    Only proves my point that leading zeros = unexpected behavior.

    Interesting tho converting to an integer doesn't have this problem as the leading zeros are dropped.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  8. #8
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    $input = 001000;
    echo gettype($input);

    This gives result me "INTEGER" as value type though.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  9. #9
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    001000 is a octal value which translates to decimal 512. When multiplied by 2 it becomes 1024. So no surprises up there.

  10. #10
    SitePoint Wizard bronze trophy Kailash Badu's Avatar
    Join Date
    Nov 2005
    Posts
    2,560
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug
    This gives result me "INTEGER" as value type though.
    Off course it would. An octal whole number is an Integer. So is the Hex and decimal equivalent.


Bookmarks

Posting Permissions

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