How to Set Price Format using JavaScript in Magento 2

The Magento 2 price format at checkout can sometimes be confusing in case of any additional charges such as VAT, surcharge, payment fee, etc.

Earlier, I had offered a php-based solution to get formatted price with currency in Magento 2.

Today, I am here with a JS-based solution for the same!

The default Magento 2 will not bother about displaying the currency symbol or decimal values but simply display the numbers. It will look something like this:

So if you would want to display $20.99, instead it will show 2099. The meaning will entirely change in such cases.

How to Set Price Format using JavaScript in Magento 2 1

However, after applying the solution to set price format using Javascript in Magento 2, it will display the additional charges as below:

How to Set Price Format using JavaScript in Magento 2 2

Check the solution for the same:

Method to Set Price Format Using Javascript in Magento 2:

define(
    [
        'ko',
        'jquery',
        'Magento_Checkout/js/view/summary/abstract-total',
        'Magento_Checkout/js/model/quote',
        'Magento_Catalog/js/price-utils'
    ],
    function (ko,$,Component,quote,priceUtils) {
        "use strict";
        return Component.extend({
            getPrice : function () {
                var price = 20; //  You can giv date
                return priceUtils.formatPrice(price, quote.getBasePriceFormat());
            } 
        });
    }
);

That’s it.

Do share the post with the Magento Community via social media.

Thank you.

Table of Contents