T4VN is an online PHP Help community that provides PHP Tutorials, PHP Examples, PHP Scripts, PHP Support
    HOME  |  HOSTING COUPON  |  TEMPLATE  |  PHP SCRIPTS  |  LINK TO US  |  LINK  |  REGISTER | CONTACT
::: Member Login :::
 Username
 Password
 
Forgot your password ?
::: PHP Tutotal :::
  PHP Basic (7)
  PHP Advanced (4)
  PHP Database (2)
  Coding Step By Step (8)
  PHP and AJAX (0)
::: PHP Example :::
  Arrays (1)
  Code Highlighters (3)
  Database Functions (12)
  Date & Time (5)
  E-Mail (6)
  Forms (5)
  Guestbooks (1)
  Logging (2)
  Miscellaneous (10)
  Password Generators (3)
  Randomizers (3)
  String Manipulation (10)
  User Authentication (6)
::: Search On T4VN :::
::: T4VN Statistics :::
  PHP Scripts : 61
  PHP Example : 67
  PHP Tutorials : 21
  PHP News : 93
  Total Coupon : 36
  Other Tutorials :
  Member : 209
  Visitor Online : 6
  Today Visit: 386
  Total Visitor : 262396
  Most Online : 31
::: Sponsored Links :::

PR 4 For This Webpage
Home Today :

Numeric Currency to Alpha Word Value String

Author : BeastRider

This function takes a numeric currency value - say 34.87, and converts it to a string as in Thirty Four Dollars and Eighty Seven Cents. Handles amounts up to 999 million. Handles singular and plural "dollars" and "cents". Decently documented and easy to follow.

PHP Example :

<?php

//*************************************************************
// this function converts an amount into alpha words
// with the words dollars and cents.  Pass it a float.
// Example:  $3.77 = Three Dollars and Seventy Seven Cents
// works up to 999,999,999.99 dollars - Great for checks
//*************************************************************

function makewords($numval)
{
$moneystr "";
// handle the millions
$milval = (integer)($numval 1000000);
if(
$milval 0)
  {
  
$moneystr getwords($milval) . " Million";
  }

// handle the thousands
$workval $numval - ($milval 1000000); // get rid of millions
$thouval = (integer)($workval 1000);
if(
$thouval 0)
  {
  
$workword getwords($thouval);
  if (
$moneystr == "")
    {
    
$moneystr $workword " Thousand";
    }
  else
    {
    
$moneystr .= " " $workword " Thousand";
    }
  }

// handle all the rest of the dollars
$workval $workval - ($thouval 1000); // get rid of thousands
$tensval = (integer)($workval);
if (
$moneystr == "")
  {
  if (
$tensval 0)
    {
    
$moneystr getwords($tensval);
    }
  else
    {
    
$moneystr "Zero";
    }
  }
else 
// non zero values in hundreds and up
  
{
  
$workword getwords($tensval);
  
$moneystr .= " " $workword;
  }

// plural or singular 'dollar'
$workval = (integer)($numval);
if (
$workval == 1)
  {
  
$moneystr .= " Dollar And ";
  }
else
  {
  
$moneystr .= " Dollars And ";
  }

// do the pennies - use printf so that we get the
// same rounding as printf
$workstr sprintf("%3.2f",$numval); // convert to a string
$intstr substr($workstr,strlen 22);
$workint = (integer)($intstr);
if (
$workint == 0)
  {
  
$moneystr .= "Zero";
  }
else
  {
  
$moneystr .= getwords($workint);
  }
if (
$workint == 1)
  {
  
$moneystr .= " Cent";
  }
else
  {
  
$moneystr .= " Cents";
  }

// done - let's get out of here!
return $moneystr;
}

//*************************************************************
// this function creates word phrases in the range of 1 to 999.
// pass it an integer value
//*************************************************************
function getwords($workval)
{
$numwords = array(
  
=> "One",
  
=> "Two",
  
=> "Three",
  
=> "Four",
  
=> "Five",
  
=> "Six",
  
=> "Seven",
  
=> "Eight",
  
=> "Nine",
  
10 => "Ten",
  
11 => "Eleven",
  
12 => "Twelve",
  
13 => "Thirteen",
  
14 => "Fourteen",
  
15 => "Fifteen",
  
16 => "Sixteen",
  
17 => "Seventeen",
  
18 => "Eightteen",
  
19 => "Nineteen",
  
20 => "Twenty",
  
30 => "Thirty",
  
40 => "Forty",
  
50 => "Fifty",
  
60 => "Sixty",
  
70 => "Seventy",
  
80 => "Eighty",
  
90 => "Ninety");

// handle the 100's
$retstr "";
$hundval = (integer)($workval 100);
if (
$hundval 0)
  {
  
$retstr $numwords[$hundval] . " Hundred";
  }

// handle units and teens
$workstr "";
$tensval $workval - ($hundval 100); // dump the 100's
if (($tensval 20) && ($tensval 0))// do the teens
  
{
  
$workstr $numwords[$tensval];
  }
else 
// got to break out the units and tens
  
{
  
$tempval = ((integer)($tensval 10)) * 10// dump the units
  
$workstr $numwords[$tempval]; // get the tens
  
$unitval $tensval $tempval// get the unit value
  
if ($unitval 0)
    {
    
$workstr .= " " $numwords[$unitval];
    }
  }

// join all the parts together and leave
if ($workstr != "")
  {
  if (
$retstr != "")
    {
    
$retstr .= " " $workstr;
    }
  else
    {
    
$retstr $workstr;
    }
  }
return 
$retstr;
}

$floatval 42341.55;
$myresult makewords($floatval);
echo 
"$myresult<br>";
printf("The value is %0.2f<br>",$floatval);

$floatval 0.63;
$myresult makewords($floatval);
echo 
"<br>$myresult<br>";
printf("The value is %0.2f<br>",$floatval);

$floatval 1.00;
$myresult makewords($floatval);
echo 
"<br>$myresult<br>";
printf("The value is %0.2f<br>",$floatval);

$floatval 0.01;
$myresult makewords($floatval);
echo 
"<br>$myresult<br>";
printf("The value is %0.2f<br>",$floatval);

$floatval 76842341.55;
$myresult makewords($floatval);
echo 
"<br>$myresult<br>";
printf("The value is %0.2f<br>",$floatval);

$floatval 0.00;
$myresult makewords($floatval);
echo 
"<br>$myresult<br>";
printf("The value is %0.2f<br>",$floatval);

$floatval 11.04;
$myresult makewords($floatval);
echo 
"<br>$myresult<br>";
printf("The value is %0.2f<br>",$floatval);

$floatval 456.78;
$myresult makewords($floatval);
echo 
"<br>$myresult<br>";
printf("The value is %0.2f<br>",$floatval);

$floatval 76.97;
$myresult makewords($floatval);
echo 
"<br>$myresult<br>";
printf("The value is %0.2f<br>",$floatval);

$floatval 777777777.77;
$myresult makewords($floatval);
echo 
"<br>$myresult<br>";
printf("The value is %0.2f<br>",$floatval);
?>


Other Example
Shorten Variables Longer Than $x Characters
Remove Images Using PHP From a String
String Insertion Functions
Apache-Style Event Log Function
Correct string endings
Template Class


::: Resources :::
  Links Directory
  Programming 2 3
  Webmaster 2 3
  Web Design 2 3
  Web Hosting 2 3
  Other Links 2 3
  Asian ShowBiz News
  Teach Seo For You
::: New Templates :::




::: Other Tutorials :::
 Program Design

  Powered By T4VN.NET - Version 2.0 - CopyRight © T4VN.NET 2005-2007