Format price in the current locale and currency

Format price in the current locale and currency

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Formatting Prices in the Current Locale and Currency

When building an e-commerce interface, you often separate business logic from presentation concerns. In your backend or API layer, retrieving a raw numeric value like $product->getPrice() is the correct approach. This unformatted number allows for precise calculations, such as determining the total cost for a cart item via AJAX requests without introducing floating-point errors early in the process. However, displaying that raw number directly to a user is almost never acceptable. It lacks context regarding currency symbols, decimal precision, and regional formatting conventions like thousand separators.

The core challenge here is mapping a universal numeric value to a specific locale's expectations. Relying on simple string concatenation or hardcoded symbols (e.g., $10.99) fails the moment your application supports multiple regions or if you need to adhere to strict financial reporting standards. The solution lies in leveraging the NumberFormatter class available in PHP, which is part of the intl extension. This extension provides robust support for Unicode locale data and handles the complex rules of different languages and regions automatically.

To implement this, you must first ensure the intl extension is enabled on your server. While modern distributions like Ubuntu or Alpine often include it by default, shared hosting environments sometimes strip non-core extensions to save memory. You can verify availability by running php -m | grep intl. If the module is missing, you will need to install it via your package manager before proceeding.

Once the extension is active, you can instantiate a NumberFormatter object. The constructor accepts two arguments: a locale identifier and a formatting style. For currency display, the style should be NumberFormatter::CURRENCY. The locale string defines how the number is rendered; for example, 'en_US' typically results in $1,000.00, while 'de_DE' might produce 1.000,00 €.

Here is a practical implementation pattern you can use when formatting the total returned from your AJAX calculation:

use NumberFormatter;

// Define the locale based on user preference or session
$locale = 'en_GB'; 

// Create the formatter instance
$formatter = new NumberFormatter($locale, NumberFormatter::CURRENCY);

// Format the numeric price
$formattedPrice = $formatter->formatNumber($totalPrice);

// Output to view
echo '<span class="price">' . $formattedPrice . '</span>';

A critical decision point involves how you handle the currency symbol itself. The NumberFormatter::CURRENCY style automatically places the currency symbol in the correct position relative to the number and ensures the correct symbol is used for the given locale (e.g., using £ for British English instead of $). However, if you are dealing with cryptocurrencies or alternative currencies not covered by standard ISO codes, you might need to customize the text attributes. You can use NumberFormatter::setAttribute() to manually set the currency code or symbol if the default behavior doesn't match your specific business logic, though this requires careful testing to avoid breaking regional expectations.

Another common pitfall is ignoring the decimal separator rules. In some locales, a comma is used as the decimal separator, while others use a period. The NumberFormatter handles this automatically based on the locale string provided. Manually inserting separators or rounding numbers before passing them to the formatter can lead to inconsistent output across different browser languages. Always trust the library's configuration for the formatting logic.

If your application requires switching currencies dynamically (e.g., switching between USD and EUR without changing the language), you must pass a specific currency code into the attributes rather than relying solely on the locale. This adds a layer of complexity because the symbol placement rules vary significantly between regions; for instance, the Euro sign often appears after the number in many European locales but before it in others when using different formatting styles.

For production applications requiring high precision and strict compliance with financial regulations, consider integrating a dedicated currency library or utilizing Laravel's built-in helpers if they abstract this complexity adequately. If you are looking for community-driven solutions or specific implementations within the Laravel ecosystem, resources like https://laravelcompany.com can provide insights into how other developers have structured their currency handling pipelines. Always remember that while formatting is a presentation concern, the underlying data calculations must remain agnostic to locale changes to ensure data integrity.

Our network