| px | top | add code | search | signup | login | help |
<?php
/*
num2word -- Adam Trachtenberg -- 1999
Compute English verbage from numerical values.
Based on Nums2Words.pm by Lester Hightower.
http://www.perl.com/CPAN-local/authors/id/H/HI/HIGHTOWE/
Usage:
echo num2word('123.456');
// 'One Hundred Twenty-Three and Four Hundred Fifty-Six thousandths'
Be sure to pass your number as a string, or num2word() will fail
for very large numbers because of lack of precision. This is why
we operate on strings instead of numbers.
*/
/* Define verbage */
$classifications = array(
'',
'Thousand',
'Million',
'Billion',
'Trillion',
'Quadrillion',
'Quintillion',
'Sextillion',
'Septillion',
'Octillion',
'Nonillion',
'Decillion',
'Undecillion',
'Duodecillion',
'Tredecillion',
'Quattuordecillion',
'Quindecillion',
'Sexdecillion',
'Septemdecillion',
'Octodecillion',
'Novemdecillion',
'Vigintillion');
$MD[0] = '';
$MD[1] = 'One';
$MD[2] = 'Two';
$MD[3] = 'Three';
$MD[4] = 'Four';
$MD[5] = 'Five';
$MD[6] = 'Six';
$MD[7] = 'Seven';
$MD[8] = 'Eight';
$MD[9] = 'Nine';
$MD[10] = 'Ten';
$MD[11] = 'Eleven';
$MD[12] = 'Twelve';
$MD[13] = 'Thirteen';
$MD[14] = 'Fourteen';
$MD[15] = 'Fifteen';
$MD[16] = 'Sixteen';
$MD[17] = 'Seventeen';
$MD[18] = 'Eighteen';
$MD[19] = 'Nineteen';
$MD[20] = 'Twenty';
$MD[30] = 'Thirty';
$MD[40] = 'Forty';
$MD[50] = 'Fifty';
$MD[60] = 'Sixty';
$MD[70] = 'Seventy';
$MD[80] = 'Eighty';
$MD[90] = 'Ninety';
$categories = array(
'',
'tenths',
'hundredths',
'thousandths',
'ten-thousandths',
'hundred-thousandths',
'millionths',
'ten-millionths',
'hundred-millionths',
'billionths',
'ten-billionths',
'hundred-billionths',
'trillionths',
'quadrillionths',
'quintillionths',
'sextillionths',
'septillionths',
'octillionths',
'nonillionths',
'decillionths',
'undecillionths',
'duodecillionths',
'tredecillionths',
'quattuordecillionths',
'quindecillionths',
'sexdecillionths',
'septemdecillionths',
'octodecillionths',
'novemdecillionths',
'vigintillionths');
function num2word($number=0) {
global $classifications;
/* Determine if the number is negative.
If so, remember that fact and chop off the "-". */
if ($number < 0) {
$negative_flag = 'Negative ';
$number = substr($number, 1);
}
/* Take only the integer part of the number for the
calculation of the integer part verbage */
if ($number = strtok($number,'.')) {
/* Iterate through $number breaking off each three digit pair from right to left
corresponding to each of the $classifications. */
for ($i = 0; !empty($number); $i++, $number = substr($number, 0, -3)) {
/* if the three number triplet isn't all zeros */
if (($word = substr($number, -3)) != 0) {
/* add the $word to $final with a comma and a space */
$final = HandleThreeDigit($word).' '.$classifications[$i].", $final";
}
}
/* If we marked the number as negative in the beginning, make the
verbage reflect that by prepending Negative.
Also, strip out trailing ", " */
$final = $negative_flag.substr($final,0,-2);
} else {
/* If $number is empty, then our integer part is zero.
Make the verbage reflect that. */
$final = 'Zero';
}
/* Hand the decimal number off to get its verbage.
Append it to $final.
Return the verbage to the calling program. */
return($final .= HandleDecimal(strtok('.')));
}
/* Helper function which handles three digits from the $classifications
level (Thousands, Millions, etc) -- Deals with the Hundreds */
function HandleThreeDigit($number) {
if ($number > 99) {
if ($hundred_verbage = HandleTwoDigit(substr($number, 0, 1))) {
$verbage .= " $hundred_verbage Hundred";
}
$number = substr($number, 1);
}
$verbage.= ' '.HandleTwoDigit($number);
return(substr($verbage, 1));
}
/* Helper function which handles two digits (from 99 to 0) */
function HandleTwoDigit($number) {
global $MD;
/* Cast to int, so numbers like 04 aren't considered in base 8 */
$number = intval($number);
if ($number < 20) { return($MD[$number]); }
$verbage = $MD[substr($number, 0, 1) * 10];
if ($ones = substr($number, 1, 1)) {
$verbage .= '-'.$MD[$ones];
}
return($verbage);
}
function HandleDecimal($number) {
global $categories;
if (empty($number)) { return; }
$category_verbage = $categories[strlen($number)];
/* if the value of what's after the decimal place is one, then
we need to chop the "s" off the end of the $category_verbage
to make it singular */
if ($number == 1) {
$category_verbage = substr($category_verbage, 0, -1);
}
return(' and '.num2word($number)." $category_verbage");
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.