Alternative to money_format() Function in PHP on Windows Platform
If you have the Intl extension, you can use
- NumberFormatter::formatCurrency — Format a currency value according to the formatter rules.
Example from Manual
$fmt = new NumberFormatter( 'de_DE', NumberFormatter::CURRENCY );
echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n";
echo $fmt->formatCurrency(1234567.891234567890000, "RUR")."\n";
$fmt = new NumberFormatter( 'ru_RU', NumberFormatter::CURRENCY );
echo $fmt->formatCurrency(1234567.891234567890000, "EUR")."\n";
echo $fmt->formatCurrency(1234567.891234567890000, "RUR")."\n";
Output
1.234.567,89 €
1.234.567,89 RUR
1 234 567,89€
1 234 567,89р.
Also see my answer on how to parse that formatted money string back into a float:
- PHP: unformat money
Keep it simple!
sprintf('%01.2f', $val);
<?php
function toMoney($val,$symbol='$',$r=2)
{
$n = $val;
$c = is_float($n) ? 1 : number_format($n,$r);
$d = '.';
$t = ',';
$sign = ($n < 0) ? '-' : '';
$i = $n=number_format(abs($n),$r);
$j = (($j = $i.length) > 3) ? $j % 3 : 0;
return $symbol.$sign .($j ? substr($i,0, $j) + $t : '').preg_replace('/(\d{3})(?=\d)/',"$1" + $t,substr($i,$j)) ;
}
echo toMoney(9856478521456.256);
?>
try this the out put of above code is "$9,856,478,521,456.26"