Formatting Prices in Indian Standard Using JavaScript

Posted by

Formatting Price in Indian Standard using JavaScript

How to format price in Indian standard in JavaScript?

If you are dealing with prices in your JavaScript application and need to format them according to the Indian standard, there are a few ways you can achieve this. In this article, we will explore how to format a price in Indian format using JavaScript.

Using toLocaleString()

The toLocaleString() method in JavaScript can be used to format a number into a currency string based on the specified locale. We can use this method to format the price in Indian standard.

    
    var price = 1234567.89;
    var formattedPrice = price.toLocaleString('en-IN', {style: 'currency', currency: 'INR'});
    console.log(formattedPrice); // Output: ₹ 12,34,567.89
    
    

Custom Function

If you prefer a custom solution, you can create a function to format the price according to the Indian standard. Here is an example of a simple function to achieve this:

    
    function formatPrice(price) {
        var parts = price.toString().split('.');
        var formattedPrice = 'u20B9 ' + parts[0].replace(/B(?=(d{2})+(?!d))/g, ',') + '.' + parts[1];
        return formattedPrice;
    }

    var price = 1234567.89;
    var formattedPrice = formatPrice(price);
    console.log(formattedPrice); // Output: ₹ 12,34,567.89
    
    

These are just a couple of ways to format a price in Indian standard using JavaScript. You can choose the method that best suits your application and use case.