PHP: Convert number into Text (Amount into words)

This is a php function which converts number into text. This function is developed in respect of Bangladeshi Currency (Bangladeshi Taka), but if you know a little php, you can convert it into your currency. Just call the function as

[php]

[/php]

You will get the result.

Code:
[php]
<?php
function convert_number($number)
{
if (($number 999999999))
{
throw new Exception(“Number is out of range”);
}

$Gn = floor($number / 100000); /* Millions (giga) */
$number -= $Gn * 100000;
$kn = floor($number / 1000); /* Thousands (kilo) */
$number -= $kn * 1000;
$Hn = floor($number / 100); /* Hundreds (hecto) */
$number -= $Hn * 100;
$Dn = floor($number / 10); /* Tens (deca) */
$n = $number % 10; /* Ones */

$res = “”;

if ($Gn)
{
$res .= convert_number($Gn) . ” Lacs”;
}

if ($kn)
{
$res .= (empty($res) ? “” : ” “) .
convert_number($kn) . ” Thousand”;
}

if ($Hn)
{
$res .= (empty($res) ? “” : ” “) .
convert_number($Hn) . ” Hundred”;
}

$ones = array(“”, “One”, “Two”, “Three”, “Four”, “Five”, “Six”,
“Seven”, “Eight”, “Nine”, “Ten”, “Eleven”, “Twelve”, “Thirteen”,
“Fourteen”, “Fifteen”, “Sixteen”, “Seventeen”, “Eightteen”,
“Nineteen”);
$tens = array(“”, “”, “Twenty”, “Thirty”, “Fourty”, “Fifty”, “Sixty”,
“Seventy”, “Eigthy”, “Ninety”);

if ($Dn || $n)
{
if (!empty($res))
{
$res .= ” and “;
}

if ($Dn
[/php]

Leave a Reply

%d bloggers like this: