In PHP, the money_format function is a commonly used tool to format numbers into currency format. It adjusts the currency symbol, separators, and decimal places based on the system's locale settings. This function makes it easy to format numbers with thousand separators in currency format.
<span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-title function_ invoke__">money_format</span></span><span> ( </span><span><span class="hljs-keyword">string</span></span><span> </span><span><span class="hljs-variable">$format</span></span><span> , </span><span><span class="hljs-keyword">float</span></span><span> </span><span><span class="hljs-variable">$number</span></span><span> )
</span></span>
$format: A format string that typically includes a currency symbol (like $, €, etc.) along with formatting characters for thousand separators and decimals.
$number: The number you want to format.
Before using the money_format function, you need to set the correct locale to ensure the output matches your desired currency format. PHP uses the setlocale function to configure the locale.
<span><span><span class="hljs-title function_ invoke__">setlocale</span></span><span>(LC_MONETARY, </span><span><span class="hljs-string">'en_US.UTF-8'</span></span><span>); </span><span><span class="hljs-comment">// Set to US English locale, using dollar sign and thousand separator</span></span><span>
</span></span>
In this line, we set the locale to US English en_US.UTF-8, meaning all currency-related formatting will follow US standards including the dollar sign $, comma , as the thousand separator, and period . for decimals.
Next, you can use money_format to format an amount. For example, to format a number as currency:
<span><span><span class="hljs-variable">$amount</span></span><span> = </span><span><span class="hljs-number">1234567.89</span></span><span>;
</span><span><span class="hljs-variable">$formattedAmount</span></span><span> = </span><span><span class="hljs-title function_ invoke__">money_format</span></span><span>(</span><span><span class="hljs-string">'%.2n'</span></span><span>, </span><span><span class="hljs-variable">$amount</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$formattedAmount</span></span><span>; </span><span><span class="hljs-comment">// Outputs: $1,234,567.89</span></span><span>
</span></span>
The format string '%.2n' means:
%.2: Keep two decimal places.
n: Output the number with currency symbol, formatted based on the current locale.
If you want to format the amount with different currency symbols (like Euro, Pound, etc.), just change the locale setting. For example:
<span><span><span class="hljs-title function_ invoke__">setlocale</span></span><span>(LC_MONETARY, </span><span><span class="hljs-string">'de_DE.UTF-8'</span></span><span>); </span><span><span class="hljs-comment">// Set to German locale, using Euro symbol</span></span><span>
</span><span><span class="hljs-variable">$formattedAmount</span></span><span> = </span><span><span class="hljs-title function_ invoke__">money_format</span></span><span>(</span><span><span class="hljs-string">'%.2n'</span></span><span>, </span><span><span class="hljs-variable">$amount</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$formattedAmount</span></span><span>; </span><span><span class="hljs-comment">// Outputs: 1.234.567,89 €</span></span><span>
</span></span>
Under the German locale, the currency format changes: the thousand separator becomes ., the decimal separator becomes ,, and the currency symbol is €.
money_format is only available on systems that support it. Typically, it's available on Unix-like systems (like Linux and macOS), but not on Windows. If you're developing on Windows, consider using other formatting methods such as number_format.
money_format automatically adapts the format based on the current locale, so you may see different results depending on the locale settings.
If you don't want to rely on the system's locale settings, or if you're working in an environment where money_format is not supported, you can use PHP's number_format function to manually format amounts and add thousand separators:
<span><span><span class="hljs-variable">$formattedAmount</span></span><span> = </span><span><span class="hljs-string">'$'</span></span><span> . </span><span><span class="hljs-title function_ invoke__">number_format</span></span><span>(</span><span><span class="hljs-variable">$amount</span></span><span>, </span><span><span class="hljs-number">2</span></span><span>, </span><span><span class="hljs-string">'.'</span></span><span>, </span><span><span class="hljs-string">','</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$formattedAmount</span></span><span>; </span><span><span class="hljs-comment">// Outputs: $1,234,567.89</span></span><span>
</span></span>
The number_format function takes the following parameters:
First parameter: the number.
Second parameter: number of decimal places.
Third parameter: decimal point symbol (usually .).
Fourth parameter: thousand separator (usually ,).
PHP's money_format function is a very convenient tool for handling currency formatting. It automatically adjusts the format based on locale settings, saving you from a lot of manual work. However, make sure to check if your development environment supports this function. If not, you can use number_format as an alternative.