<span><span><span class="hljs-meta"><?php</span></span><span>
</span><span><span class="hljs-comment">// This article only provides sample code demonstrations and is unrelated to the main content.</span></span><span>
</span><span><span class="hljs-comment">// Adjust according to your actual use case.</span></span><span>
<p></span>setlocale(LC_MONETARY, 'en_US.UTF-8');<br>
$amount = 1234567.89;<br>
echo money_format('%i', $amount);</p>
<p>?></p>
<hr>
<p># How to Properly Use PHP's money_format Function with setlocale for Localized Currency Formatting<span></p>
<p>In financial or e-commerce application development, currency formatting is a common requirement. PHP's <code>money_format
The code above sets the currency format to US English (UTF-8 encoding).
money_format
Formats currency values according to the current locale.
For example:
<span><span><span class="hljs-variable">$amount</span></span><span> = </span><span><span class="hljs-number">1234.56</span></span><span>;
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">money_format</span></span><span>(</span><span><span class="hljs-string">'%i'</span></span><span>, </span><span><span class="hljs-variable">$amount</span></span><span>);
</span></span>
Under the US locale, the output will be: USD 1234.56.
The first parameter of money_format is the format control string. Common options include:
%i: Outputs currency in the international format with a currency symbol according to the locale.
%n: Outputs currency in the national format according to the locale.
%!n: Omits the currency symbol, showing only the numeric value.
Example:
<span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">money_format</span></span><span>(</span><span><span class="hljs-string">'%n'</span></span><span>, </span><span><span class="hljs-number">1234.56</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-title function_ invoke__">money_format</span></span><span>(</span><span><span class="hljs-string">'%i'</span></span><span>, </span><span><span class="hljs-number">1234.56</span></span><span>);
</span></span>
Key points to note:
money_format is not available on Windows. It works mainly on Unix-like systems such as Linux and macOS.
On Windows or in newer PHP versions, using NumberFormatter (Intl extension) is recommended for cross-platform currency formatting.
Example:
<span><span><span class="hljs-variable">$fmt</span></span><span> = </span><span><span class="hljs-keyword">new</span></span><span> </span><span><span class="hljs-title class_">NumberFormatter</span></span><span>(</span><span><span class="hljs-string">'en_US'</span></span><span>, </span><span><span class="hljs-title class_">NumberFormatter</span></span><span>::</span><span><span class="hljs-variable constant_">CURRENCY</span></span><span>);
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-variable">$fmt</span></span><span>-></span><span><span class="hljs-title function_ invoke__">formatCurrency</span></span><span>(</span><span><span class="hljs-number">1234.56</span></span><span>, </span><span><span class="hljs-string">'USD'</span></span><span>);
</span></span>
Suppose you are developing an international e-commerce system and need to display currency differently based on the user's country. You can do it like this:
<span><span><span class="hljs-variable">$locale</span></span><span> = </span><span><span class="hljs-string">'fr_FR.UTF-8'</span></span><span>;
</span><span><span class="hljs-title function_ invoke__">setlocale</span></span><span>(LC_MONETARY, </span><span><span class="hljs-variable">$locale</span></span><span>);
<p></span>$amount = 987654.32;<br>
echo money_format('%i', $amount);<br>
</span>
The output may be: 987 654,32 EUR (French formatting style).
Use setlocale to control the localization rules for currency.
On Unix-like systems, money_format can be used for quick localized currency output.
On Windows or for cross-platform needs, use the Intl extension with NumberFormatter.
Always clarify the target platform and user locale settings to avoid function failures due to non-existent locales.
By correctly pairing money_format with setlocale, you can significantly enhance the user experience across different regions, making currency display align with local reading habits.