Home » Php » Alternative to money_format() Function in PHP on Windows Platform

Alternative to money_format() Function in PHP on Windows Platform

Posted by: admin April 23, 2020 Leave a comment

Questions:

I am using the money_format() function in PHP, which gives the following error:

Fatal error: Call to undefined function money_format()

Searches about this error reveal that the function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.

Is there an equivalent PHP function available for Windows?

How to&Answers:

If you have the Intl extension, you can use

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:

Answer:

Keep it simple!

sprintf('%01.2f', $val);

Answer:

<?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”

Answer:

I would suggest taking a look into NumberFormatter using NumberFormatter::CURRENCY and locale.

Also Laravel number_format($price, 2) function is super useful.

Note, also useful functional that seems to contain similar format:

And don’t forget to check if the function exist!

Answer:

output:$45.00

Answer:

I always just used this to manually add the dollar sign:

Answer:

The solution to this problem is create a PHP file with the money_format() function and use the Apache auto_prepend_file directive in your php.ini file. Usually, if you use XAMPP, your php.ini should be in

Append this line of code to your php.ini file.

use this function in money_format.php

Answer:

@Ajeet toMoney function looks good, but it is not working for the ‘0899’

Change length Into strlen()

$j = (($j = $i.length) > 3) ? $j % 3 : 0;

so change into below like

$j = (($j = strlen($i)) > 3) ? $j % 3 : 0;

Now this will work for any data.

Answer:

I don’t understand why @Ajeet is making it so complicated why not do like this,
It also now works for 4 digit numbers to answer @bharanikumar “but it is not working for the ‘0899’”

Answer:

@Y Talansky your function could be the next code:

Answer:

**

CLP money (moneda peso chileno, con formato miles)

**

Answer:

I use this function:

And use it like this:

Answer:

Instead of money_format() you can also try this. Hope this will be helped any guy who faced this call to undefined function money_format() error.

Output: