Skip to content

Instantly share code, notes, and snippets.

@techjewel
Last active April 14, 2023 20:51
Show Gist options
  • Save techjewel/6645397 to your computer and use it in GitHub Desktop.
Save techjewel/6645397 to your computer and use it in GitHub Desktop.
Convert Numbers to Words - for Bangladeshi counting system
<?php
/**
* Function: convert_number
*
* Description:
* Converts a given integer (in range [0..1T-1], inclusive) into
* alphabetical format ("one", "two", etc.)
*
* @int
*
* @return string
*
*/
function convert_number($number)
{
$my_number = $number;
if (($number < 0) || ($number > 999999999))
{
throw new Exception("Number is out of range");
}
$Kt = floor($number / 10000000); /* Koti */
$number -= $Kt * 10000000;
$Gn = floor($number / 100000); /* lakh */
$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 ($Kt)
{
$res .= convert_number($Kt) . " Koti ";
}
if ($Gn)
{
$res .= convert_number($Gn) . " Lakh";
}
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 < 2)
{
$res .= $ones[$Dn * 10 + $n];
}
else
{
$res .= $tens[$Dn];
if ($n)
{
$res .= "-" . $ones[$n];
}
}
}
if (empty($res))
{
$res = "zero";
}
return $res;
}
$cheque_amt = 87474840;
try
{
echo $cheque_amt ." = ". convert_number($cheque_amt);
}
catch(Exception $e)
{
echo $e->getMessage();
}
?>
@bhaktear
Copy link

Thanks bro 👍

@himel-ceps
Copy link

ভাই, চমৎকার একটি কাজ হয়েছে । তবে ডেসিমাল ভ্যালুর জন্য কোন অপশন নেই ? দশমিকের পরের ডিজিট তো কাউন্ট হচ্ছে না

@kashohel
Copy link

Actually I am watching for like as 123 = এক শত তেইশ
Your code help me a lot to modify and get proper output in original bangla. Modified code

@arif98741
Copy link

You have done a extra-ordinary work. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment