Zend - The PHP Company




Math

Add Code


String to money  

Type: code fragment
Added by: kcochrane
Entered: 18/07/2002
Last modified: 07/12/2001
Rating: - (fewer than 3 votes)
Views: 5996
It takes in a string like 12345.123 and it will convert it to the normal money format 12,345.12


<?php
// This is a hack on cwe7g's comma integer script.
// It takes in a string and adds comma's and also
// adds the correct cents
//
// Ken Cochrane
// Ken@PopcornMonsters.com
// http://www.PopcornMonsters.com
//
// Current Version 1.1
//
// What's new in Version 1.1
// - added support for negative numbers.

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 First and Second
   // First is before decimal, second is after 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 <= 
    { 
     
//To Short to add a comma
    
$string $First "." $Second;
    
    
// if negative flag is set, add negative to number
    
if($Negative == 1)
     {    
      
$string "-" $string;
     }
         
    return 
$string
    } 
  else 
    { 
    
$loop_count intval( ( $length ) ); 
    
$section_length = -3
    for( 
$i 0$i $loop_count$i++ ) 
      { 
      
$sections[$i] = substr$First$section_length); 
      
$section_length $section_length 3
      } 

    
$stub = ( $length );    
    if( 
$stub != 
      { 
      
$sections[$i] = substr$First0$stub ); 
      } 
    
$Done implode","array_reverse$sections ) );
    
$Done $Done "." $Second
    
   
// if negative flag is set, add negative to number
    
if($Negative == 1)
     {    
      
$Done "-" $Done;
     }

    return  
$Done;
    } 
  }
?>


Usage Example


Example 1.

$number = "12345.123";
$Money = to_money($number);
print("$ $Money");
// prints out '$ 12,345.12'
--------------------------------
Example 2.

$number = "-1345";
$Money = to_money($number);
print("$ $Money");
// prints out '$ -1,345.00'



Rate This Script





Search



This Category All Categories