SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Decimals & leading zeroes

    Hey,

    I got some numbers to perform some basic maths on. Little problem with it, is that some numbers are regular ints, while others look like this: "1,234". If I do those maths on the latter type, they'll lose everything after the comma.

    Also, they have leading zeroes. How can I keep those as well?

    Thanks in advance,

    KonTiki

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You could str_replace() the commas away.

    numbers don't really have leading zeros. So you would need to be pretty specific about what should happen to leading zeros when arithmetic is performed on a number that was extracted from a string with leading zeros. For example:

    00001 / 5 = ???
    00001 * 1000000 = ???
    01 + 0002 = ???

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2009
    Posts
    28
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Uhmmm, I need to keep the numbers behind the comma somehow. They won't change.

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    Weird things happen with numbers preceded by zeros if you are not expecting it.

    PHP Code:
    $vals[] = "1,234" ;
    $vals[] = "01234" ;
    $vals[] = 01234 ;

    var_dump$vals ) ;

    /* gives:
    array
      0 => string '1,234' (length=5)
      1 => string '01234' (length=5)
      2 => int 668
    */


    foreach( $vals as $val ){

     
    $val ltrim$val );
     
    settype($val'string');
     
    $newvals[] = str_replace"," "" $val );

    }

    var_dump$newvals );

    /* number 01234 still understood to be oct
    array
      0 => string '1234' (length=4)
      1 => string '1234' (length=4)
      2 => string '668' (length=3)
    */ 
    The more specific and varied the test data you can submit (as in $vals), the more likely you are to find a solution.


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
  •