Hey,
I wrote this little script to convert any integer greater than 0, and less than a decillion (10^33, but there are probably php integer limitations).
<?php
function makeNumberEnglish ($number) {
// Variables
$phrase = '';
$number = (string) $number;
$values = array();
$words = array();
// Fill integers array
for ($i=strlen($number)-1; $i>=0; $i-=3) {
$values[pow(10,strlen($number)-($i+1))] = (int) substr($number,($i>2)?$i-2:0,($i>2)?3:$i+1);
}
// Places and replacements (backwards to catch largest first)
$places = array_reverse ( array (
1, 1000, 1000000, 1000000000, 1000000000000, 1000000000000000, 1000000000000000000,
1000000000000000000000, 1000000000000000000000000, 1000000000000000000000000000,
1000000000000000000000000000000
) );
$placeReplacements = array_reverse ( array (
'', 'thousand', 'million', 'billion', 'trillion','quadrillion','quintillion','sextillion',
'septillion','octillion','nonillion'
) );
foreach ($values as $place => $numeral ) {
$placeWord = str_replace($places,$placeReplacements,(int) $place);
if ($numeral!=0) {
$words[$place] = makeHundredsString($numeral) . ' ' . $placeWord;
}
}
$words = array_reverse($words);
$phrase = array_shift($words);
foreach ($words as $word) {
if ($word!='') {
$phrase .= ', '.$word;
}
}
return $phrase;
}
function makeHundredsString ($number) {
if ($number >= 1000 || $number < 0) // Get rid of invalid calls
die ('makeHundredsString() may not recieve a number greater than or equal to 1,000, or less than 0.');
$words = array(); // Hold words to be concatinated
$phrase = ""; // String to concatinate words to
$number = str_pad((string) $number, 3, '0', STR_PAD_LEFT);
$numString = (string) $number;
$digits = array ('', 'one','two','three','four','five','six','seven','eight','nine');
$tens = array ('','ten','twenty','thirty','forty','fifty','sixty','seventy','eighty','ninety');
$teens = array ('ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen');
for ($i=0;$i<3;$i++) {
$words[] = ($i==1)?$tens[$numString{$i}]:$digits[$numString{$i}];
}
if ($words[1] == 'ten') {
$words[2] = $teens[$numString{2}];
$words[1] = '';
}
// Make sure it is very natural
$phrase = (($words[0]=='')?null:$words[0].' hundred ').
// If number has hundreds place, add it
(($words[0]!=''&&($words[1]!='' || $words[2]!=''))?'and ':null).$words[1].
// If number had a hundreds place, and has either ones or tens too
(($words[2]=='')?null:(($words[1]!='')?'-':null).$words[2]);
// If number is not blank: if tens place is not blank, then put dash,
// else null, then concatinate ones place
return $phrase;
}
?>
Execute it using something like this:
<?=makeNumberEnglish(522);?>
In a minute, I’ll try making an html form for y’all to test it out without running it yourself.