Use numberFormatter() function to format numbers as currency strings (money format) in JS. It is part of the Internationalization API in JavaScript.
JS money format example
Simple HTML example code create our money number formatter.
<!doctype html>
<head>
<script>
// Create our number formatter.
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
// These options are needed to round to whole numbers
//minimumFractionDigits: 0, // (this suffices for whole numbers, but will print 2500.10 as $2,500.1)
//maximumFractionDigits: 0, // (causes 2500.99 to be printed as $2,501)
});
var res = formatter.format(2500);
console.log(res)
</script>
</head>
<body>
</body>
</html>
Output:
Source: stackoverflow.com
Other country money format
Using the currency
field, you can specify which specific currency you want to format to, such as 'USD'
, 'CAD'
or 'INR'
.
const price = 1470000.15;
// Format the price above to USD, INR, EUR using their locales.
let dollarUS = Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
let rupeeIndian = Intl.NumberFormat("en-IN", {
style: "currency",
currency: "INR",
});
let euroGerman = Intl.NumberFormat("de-DE", {
style: "currency",
currency: "EUR",
});
console.log("Dollars: " + dollarUS.format(price));
// Dollars: $147,000,000.15
console.log("Rupees: " + rupeeIndian.format(price));
// Rupees: ₹14,70,000.15
console.log("Euros: " + euroEU.format(price));
// Euros: 1.470.000,15 €
Source: stackabuse.com
Do comment if you have any doubts or suggestions on this JavaScript format.
Note: The All JS Examples codes are tested on the Firefox browser and the Chrome browser.
OS: Windows 10
Code: HTML 5 Version