Today i wrote a simple code to convert a number to Bangladeshi currency (taka) format. the php format_number puts a comma (”,”) after every 3 digits which is not same for Bangladeshi currency. here is the simple function to do the job for yo. it can handle integer and decimal numbers.

function taka_format($amount = 0)
{
$tmp = explode(”.”,$amount); // for float or double values
$strMoney = “”;
$divide = 1000;
$amount = $tmp[0];
$strMoney .= str_pad($amount%$divide,3,”0″,STR_PAD_LEFT);
$amount = (int)($amount/$divide);
while($amount>0)
{
$divide = 100;
$strMoney = str_pad($amount%$divide, 2,”0″,STR_PAD_LEFT).”,”.$strMoney;
$amount = (int)($amount/$divide);
}

if(substr($strMoney, 0, 1) == “0″)
$strMoney = substr($strMoney,1);

if(isset($tmp[1])) // if float and double add the decimal digits here.
{
return $strMoney.”.”.$tmp[1];
}
return $strMoney;
}

an alternate way of doing this is via substr

function taka_format($amount = 0)
{
$tmp = explode(”.”,$amount);  // for float or double values
$strMoney = “”;
$amount = $tmp[0];
$strMoney .= substr($amount, -3,3 ) ;
$amount = substr($amount, 0,-3 ) ;
while(strlen($amount)>0)
{
$strMoney = substr($amount, -2,2 ).”,”.$strMoney;
$amount = substr($amount, 0,-2 );
}

if(isset($tmp[1]))         // if float and double add the decimal digits here.
{
return $strMoney.”.”.$tmp[1];
}
return $strMoney;
}