PHP – Currency

Discussion in 'OT Technology' started by BigPaul, Jan 15, 2007.

  1. BigPaul

    BigPaul New Member

    Joined:
    Sep 4, 2006
    Messages:
    670
    Likes Received:
    0
    I’m new to PHP and I’ve got some code that adds a bunch of numbers together, and then displays them in a table cell. <td align="center" bgcolor="#C0C0C0">$<?php echo $grouptotalvalue ?></td> the problem is there is no “,” in the output. Instead of $100,555.05 the output looks like $100555.05. Am I doing something wrong here? The numbers it is adding are all below 500.00.
     
  2. GeekDrew

    GeekDrew aka DataDrew

    Joined:
    Oct 12, 2002
    Messages:
    244
    Likes Received:
    0
    Location:
    Columbus, OH, USA
    We would need to see the code that sets $grouptotalvalue in order to help.
     
  3. crazybenf

    crazybenf Active Member

    Joined:
    Nov 14, 2001
    Messages:
    15,575
    Likes Received:
    2
    Code:
    function to_money($string) 
      { 
    
       $Negative = 0;
          
       //check to see if number is negative
        if(preg_match("/^-/",$string))
        {
         //setflag
         $Negative = 1;
         //remove negative sign
         $string = preg_replace("|-|","",$string);
        }
    
       //look for commas in the string and remove them.    
       $string = preg_replace("|,|","",$string);
       
       // split the string into two parts First and Second
       // First is before decimal, second is after. format = First.Second
       $Full = split("[.]",$string); 
      
       $Count = count($Full);
        
       if($Count > 1)
       {
        $First = $Full[0];
        $Second = $Full[1];
         $NumCents = strlen($Second);
          if($NumCents == 2)
           {
               //do nothing already at correct length
           }
          else if($NumCents < 2)
           {
               //add an extra zero to the end
               $Second = $Second . "0";
           }
          else if($NumCents > 2)
           {
               //either string off the end digits or round up
               // I say string everything but the first 3 digits and then round
            // since it is rare that anything after 3 digits effects the round
            // you can change if you need greater accurcy, I don't so I didn't
               // write that into the code.
               $Temp = substr($Second,0,3);
               $Rounded = round($Temp,-1);
               $Second = substr($Rounded,0,2);
               
           }  
    
       }
       else
       {
        //there was no decimal on the end so add to zeros    
        $First = $Full[0];    
        $Second = "00";
       }
    
      $length = strlen($First);
    
      if( $length <= 3 ) 
        { 
         //To Short to add a comma
        //combine the first part and the second.
        $string = $First . "." . $Second;    
    
        if($Negative == 1)
         {    
          $string = "-" . $string;
         }
    
        return $string; 
        } 
      else 
        { 
        $loop_count = intval( ( $length / 3 ) ); 
        $section_length = -3; 
        for( $i = 0; $i < $loop_count; $i++ ) 
          { 
          $sections[$i] = substr( $First, $section_length, 3 ); 
          $section_length = $section_length - 3; 
          } 
    
        $stub = ( $length % 3 );    
        if( $stub != 0 ) 
          { 
          $sections[$i] = substr( $First, 0, $stub ); 
          } 
        $Done = implode( ",", array_reverse( $sections ) );
        $Done = $Done . "." . $Second; 
    
        if($Negative == 1)
         {    
          $Done = "-" . $Done;
         }
    
        return  $Done;
        } 
      } 
    ?>
     
  4. GeekDrew

    GeekDrew aka DataDrew

    Joined:
    Oct 12, 2002
    Messages:
    244
    Likes Received:
    0
    Location:
    Columbus, OH, USA
    Or that. I suppose I should complain since I'm new to this forum, I'm used to giving people help figuring it out, not giving them a function that does everything for them. ;)
     
  5. crazybenf

    crazybenf Active Member

    Joined:
    Nov 14, 2001
    Messages:
    15,575
    Likes Received:
    2
    dissecting code is how I learned to code :)
     
  6. o2

    o2 Witty Title Here OT Supporter

    Joined:
    Oct 4, 2005
    Messages:
    16,099
    Likes Received:
    12
    Location:
    Toronto
  7. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
  8. crazybenf

    crazybenf Active Member

    Joined:
    Nov 14, 2001
    Messages:
    15,575
    Likes Received:
    2
    Learn something new every day. Thanks. :cool:
     
  9. BigPaul

    BigPaul New Member

    Joined:
    Sep 4, 2006
    Messages:
    670
    Likes Received:
    0
    Thanks guys!

    money_format is what i was looking for. i searched for it on PHP.net but was using the word "currency" instead of money.

    Thanks again...

    now I just need to learn some SQL and try to get my app online :bigthumb:
     

Share This Page