converting numbers into comma format
by Mohan[ Edit ] 2014-01-28 19:19:17
<h1>converting numbers into comma format </h1>
Php Built In function:
<?php
$amount = '10000068969';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;
?>
without builtin function:
-------------------------
<?php
$inramount = '65468738907776';
$inramount = moneyFormatIn($inramount);
echo $inramount;
function moneyFormatIn($num)
{
$explrestunits = "" ;
$num_array = explode(".", $num);
$num = $num_array[0];
$decimal_places = $num_array[1];
if(strlen($num)>3)
{
$lastthree = substr($num, strlen($num)-3, strlen($num));
$restunits = substr($num, 0, strlen($num)-3);
$restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits;
$expunit = str_split($restunits, 2);
for($i=0; $i<sizeof($expunit); $i++)
{
if($i==0)
{
$explrestunits .= (int)$expunit[$i].",";
}else{
$explrestunits .= $expunit[$i].",";
}
}
$thecash = $explrestunits.$lastthree;
} else {
$thecash = $num;
}
if($decimal_places=="")
$thecash = $thecash.".00";
else
$thecash = $thecash.".".substr($decimal_places,0,2);
return $thecash;
}
?>