GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum > PHP Code Library
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Jan-2003, 17:56
Elmseeker's Avatar
Elmseeker Elmseeker is offline
Awaiting Email Confirmation
 
Join Date: Jan 2003
Posts: 87
Elmseeker is on a distinguished road

[Function] Int to Money Conversion


PHP Code:

// Name: to_money
// Version: 0.1
// Author: Elmseeker
// Desription: splits a string and inserts commas and the
// correct cents to make the common Money Format $1,234.56
// Type: Function

function to_money($string)
  {  
     
   $Negative = 0;
       
   // Make sure the number is not negative .
    if(preg_match("/^\-/",$string)) 
    { 
     $Negative = 1;
     $string = preg_replace("|\-|","",$string);
    } 
      
// Make sure there aren't already commas in the string.
   $string = preg_replace("|\,|","",$string); 
    
   $Full = split("[\.]",$string);
   
   $Count = count($Full);
     
   if($Count > 1)
   { 
    $First = $Full[0];
    $Second = $Full[1];
     $NumDec = strlen($Second);
      if($NumDec == 2) 
       { 
           //do nothing already at correct length
       } 
      else if($NumDec < 2)
       { 
           //add an extra zero to the end
           $Second = $Second . "0"; 
       } 
      else if($NumDec > 2)
       { 
           // Round off the rest.
           $Temp = substr($Second,0,3);
           $Rounded = round($Temp,-1); 
           $Second = substr($Rounded,0,2);
            
       }    

   }
   else
   {   
    $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 = intval( ( $length / 3 ) );
    $section_length = -3;  
    for( $i = 0; $i < $loop; $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;
     } 
    $Done = "$" . $Done;
    return  $Done; 
    }  
  }    
       
// Usage:
// $string=1234.5612;
// $Money = to_money($string);
// print("$Money"); 
// Prints out $1,234.56 


  #2  
Old 30-Jan-2003, 23:10
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

[Function] Int to Money Conversion II


The following function uses a lot less code to do the same thing...

PHP Code:

<?php

function convert_to_money( &$s, $dec=2 )
{
  // nb. passed by reference  
  
  // converts a string / number to float  
  $f = floatval( $s );
  if( $f<0 ): // if negative
    $p = array( '($', ')' ); // formatting negative numbers
    $f = str_replace( '-', '', $f ); // removes the minus sign
  else: // positive no.
    $p = array( '$', NULL );
  endif;
  $s = $p[0].number_format( $f, $dec ).$p[1];
}

?>

 

Recent GIDBlogThe bogus security of airport screening by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
NFA to DFA conversion fiddler_808 CPP / C++ Forum 3 02-Jun-2006 12:49
Sydog's DVD to Divx Conversion Guide asanthadenz Computer Software Forum - Windows 1 14-Mar-2003 14:08
[function] Validate Email Addresses I JdS PHP Code Library 0 23-Jan-2003 03:21
[function] Timer JdS PHP Code Library 0 18-Jan-2003 18:11

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:41.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.