PHP money_format() Function
PHP money_format() Function
The money_format() function in PHP formats a number as a currency string and returns a formatted version of number.
Syntax
money_format ( string $format , float $number )
Parameter
format(required)- This parameter specifies the string to be formatted and how to format the variables in it.
number (required)- This parameter signifies the number to be inserted at the %-sign in the format string.
Return
This function returns the formatted string. It returns NULL and emits E_WARNING for Non-numeric number causes.
Example 1
Output
Number:4678.76 After using the money_format() function:4678.76
Example 2
<?php //specifying the number $num =-4678.76;// passing a negative number //printing the number echo "Number:".$num; echo "\n"; //returning a string formatted as a currency string. setlocale(LC_MONETARY,"de_DE"); echo "After using the money_format() function:".money_format("%.2n", $num); ?>
Output
Number:-4678.76 After using the money_format() function:-4678.76
Example 3
<?php //specifying the number $num =-4678.76;// passing a negative number //printing the number echo "Number:".$num; echo "\n"; //returning a string formatted as a currency string. setlocale(LC_MONETARY, 'en_US'); // Printing the international format as per the en_US locale echo "After the money_format() function:".money_format('%i', $num); ?>
Output
Number:-4678.76 After the money_format() function:-4678.76