How to Format Numbers as Currency String of Different Countries in Javascript Full Tutorial For Beginners

You are currently viewing How to Format Numbers as Currency String of Different Countries in Javascript Full Tutorial For Beginners

 

Welcome folks today in this post we will be formatting numbers as currency string of different countries in javascript. All the full source code of the application is given below.

 

 

 

Get Started

 

 

 

In order to get started we need to create an index.html file and copy paste the following code

 

index.html

 

 

First of all we will show you how to format usd currency in javascript

 

 

<!DOCTYPE html> 
<html> 

<head> 
    <title> 
        Formating number in currency string 
    </title> 
</head> 

<body> 
    <center> 
        <h1 style="color:green;">Coding Shiksha</h1> 
        <h4> 
        Formatting 4800 as USD 
        </h4> 
        <script> 
            var format = new Intl.NumberFormat('en-US', { 
                style: 'currency', 
                currency: 'USD', 
                minimumFractionDigits: 2, 
            }); 
            // for 4800 USD
            document.write(format.format(4800)); 
        </script> 
    <center> 
</body> 

</html>

 

 

Now if you open it in browser it will show currency in usd as shown below

 

 

 

 

Similiarly we can make slight modification in the program to print out the inr currency. We will change the locale attribute inside the program to en-IN for India like this

 

 

<!DOCTYPE html> 
<html> 

<head> 
    <title> 
        Formating number in currency string 
    </title> 
</head> 

<body> 
    <center> 
        <h1 style="color:green;">Coding Shiksha</h1> 
        <h4> 
        Formatting 4800 as USD 
        </h4> 
        <script> 
            var format = new Intl.NumberFormat('en-US', { 
                style: 'currency', 
                currency: 'USD', 
                minimumFractionDigits: 2, 
            }); 
            // for 4800 INR 
            document.write(format.format(4800)); 
        </script> 
    <center> 
</body> 

</html>

 

 

 

 

Leave a Reply