CopyPastehas never been so tasty!

Using money_format() function in php

by anonymous

  • 0
  • 0
  • 0

Orignal source:

Definition and Usage:


The money_format() function formatted a string as formatted it as currency string.


That function has built in feature as so it called money_formatted, it insert formatted number like($,£) in the string where there is (%) sign in the main string.


Important Note: The money_format() function of php  does not work on Windows OS.





money_format(string, number)


Both the parameters string and number are mandatory.


String: Specify the format of the string and how to format it.


Input size of Text Field:


x - Denotes the minimum field width (x). Default is 0


#x - Denotes the maximum number (x) of digits estimated to the left of the decimal point. This is used to keep formatted output aligned in the same columns. If the number of digits is larger than x, this requirement is ignored.


x - Specifies the maximum number (x) of digits estimated to the right of the decimal point. If x is 0, the decimal point and the digits to its right will not be shown.


There are three Conversion characters:


i - International currency format


n - National currency format


% - Returns the % character


If multiple characters are used then it uses it in same order in %.


Padding and Flags:


=f - Specifies the character (f) to be used as padding (Example: %=s this uses "s" as padding). Default is space



International en_US format:


Code 1:


$var = 1345.78;
setlocale(LC_MONETARY, "en_US");
echo money_format("The price is %i", $var);


Output 1:


The price is USD 1,345.78


National Norwegian format with 2 decimals:


Code 2:


$var = 1345.78;
setlocale(LC_MONETARY, "no_NO");
echo money_format("%.2n", $var);


Output 2:


NOK 1.345,78


Negative number, US national format with () to indicate negative numbers and 2 digits of right precision and "&" as fill character:


Code 3:


$var = -1345.5782;
echo money_format("%=& (#10.2n", $var);


Output 3:



Add A Comment: