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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<!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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<!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> |