Numbers To Words
This algorithm converts decimal numbers to its English word representation.
function NumbersToWords($num)
{
$words = "";
$singles = array("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine");
$teens = array("ten", "eleven", "twelve", "thirteen", "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen");
$tens = array("", "", "twenty", "thirty", "forty", "fifty", "sixty", "seventy", "eighty", "ninty");
$powers = array("", "thousand", "million", "billion", "trillion", "quadrillion", "quintillion");
if ($num >= 1000)
{
$powersLength = count($powers);
for ($i = $powersLength - 1; $i >= 0; $i--)
{
$power = pow(1000, $i);
$count = floor(($num - fmod($num, $power)) / $power);
if ($num > $power)
{
$words .= NumbersToWords($count) . " " . $powers[$i];
$num -= ($count * $power);
}
}
}
if ($num >= 100)
{
$count = floor(($num - ($num % 100)) / 100);
$words .= NumbersToWords($count) . " hundred";
$num -= ($count * 100);
}
if ($num >= 20 && $num < 100)
{
$count = floor(($num - ($num % 10)) / 10);
$words .= " " . $tens[$count];
$num -= ($count * 10);
}
if ($num >= 10 && $num < 20)
{
$words .= " " . $teens[$num - 10];
$num = 0;
}
if ($num > 0 && $num < 10)
{
$words .= " " . $singles[$num];
}
return $words;
}
Example
$data = 5478775544879599;
$value = NumbersToWords($data);
Output
five quadrillion four hundred seventy eight trillion seven hundred seventy five billion five hundred forty four million eight hundred seventy nine thousand five hundred ninty nine