Current Location: Home> Latest Articles> How to Properly Use PHP's money_format Function with setlocale for Localized Currency Formatting

How to Properly Use PHP's money_format Function with setlocale for Localized Currency Formatting

gitbox 2025-09-16
<span><span><span class="hljs-meta">&lt;?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">&#039;%i&#039;</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.

2. Common Formatting Parameters

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">&#039;%n&#039;</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">&#039;%i&#039;</span></span><span>, </span><span><span class="hljs-number">1234.56</span></span><span>);
</span></span>

3. Cross-Platform Compatibility

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">&#039;en_US&#039;</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>-&gt;</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">&#039;USD&#039;</span></span><span>);
</span></span>

4. Practical Use Case

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">&#039;fr_FR.UTF-8&#039;</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).

5. Summary and Best Practices

  1. Use setlocale to control the localization rules for currency.

  2. On Unix-like systems, money_format can be used for quick localized currency output.

  3. On Windows or for cross-platform needs, use the Intl extension with NumberFormatter.

  4. 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.