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
1 2 3 |
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
1 2 3 4 5 6 7 8 9 10 11 12 |
<?php //specifying the number $num =4678.76; //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
1 2 3 4 |
Number:4678.76 After using the money_format() function:4678.76 |
Example 2
1 2 3 4 5 6 7 8 9 10 11 12 |
<?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
1 2 3 4 |
Number:-4678.76 After using the money_format() function:-4678.76 |
Example 3
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?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
1 2 3 |
Number:-4678.76 After the money_format() function:-4678.76 |