Formating a price in Magento using JavaScript

Formating a price in Magento using JavaScript

August 27, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Formating a price in Magento using JavaScript

Formatting Prices Dynamically in Magento using JavaScript

As an experienced developer specializing in e-commerce platforms like Magento, I often encounter requests to format prices dynamically based on user interactions like quantity changes or discounts applied. In this post, we’ll dive into how to efficiently and accurately format prices in JavaScript within the context of a Magento store.

The Challenge: Formatting Prices Across Currencies

One of the primary challenges when formatting prices is ensuring compatibility across different currencies without hardcoding values specific to each one. Hardcoding values limits flexibility and can quickly become unmanageable as your Magento store grows or you expand internationally.

To overcome this, we’ll leverage Magento’s built-in libraries for handling currency formatting. This approach keeps our JavaScript code clean and maintainable while ensuring consistent pricing across all supported currencies in your store.

Formatting Prices Using JavaScript

Let’s assume you have a savings box element with an ID of price-saving that calculates the savings amount based on the regular price, discount price, and quantity selected by the user. The savings are currently being calculated correctly but aren’t formatted as currency:

// Raw savings calculation (not yet formatted)
var rawSavings = (regularPrice * quantity) - (discountPrice * quantity);

To format this number as a price in the user’s current currency, we can use Magento’s Currency.formatJs function. This function is part of the core JavaScript libraries loaded by default on your Magento storefront.

Here’s how to apply it to our savings calculation:

// Formatted savings using Currency.formatJs
var formattedSavings = Currency.formatJs(rawSavings, true);

// Update the savings box with the formatted value
document.getElementById('price-saving').innerHTML = formattedSavings;

In this example, Currency.formatJs takes two parameters:

  1. The raw number to be formatted (in this case, our calculated savings).
  2. A boolean indicating whether to include the currency symbol.

The function returns a string representing the formatted price in the user’s current currency. By using true as the second parameter, we ensure that the currency symbol is appended to the formatted value.

Leveraging Magento’s Currency Configuration

Under the hood, Currency.formatJs uses Magento’s currency configuration data stored on the frontend. This data includes the currency code and formatting rules for each supported currency in your store.

By utilizing this built-in functionality, we avoid the need to manually manage currency-specific formatting logic in our JavaScript code. As you add or remove currencies from your Magento store, the relevant formatting rules are automatically available to Currency.formatJs.

Conclusion: Formatting Prices with Ease and Flexibility

In Magento, formatting prices dynamically using JavaScript becomes a straightforward task when leveraging the platform’s built-in currency handling features.

By utilizing the Currency.formatJs function, we can efficiently format calculated price values without having to account for individual currencies or hardcode formatting rules. This approach promotes code maintainability and ensures consistent pricing across your entire storefront.

Remember to always test your JavaScript logic thoroughly, especially when dealing with user interactions like quantity changes or discounts that can affect the displayed prices.

If you’re looking for professional Magento development services to help optimize your e-commerce platform’s performance and functionality, feel free to explore our offerings at Laravel Company.

Happy coding!

Pricing JavaScript

Our network