xxxxxxxxxx
// format number as money
/*
currency values:
- Kuwaiti Dinar (KWD)
- Bahraini Dinar (BHD)
- Omani Rial (OMR)
- Jordanian Dinar (JOD)
- British Pound (GBP)
- Gibraltar Pound (GIP)
- Cayman Islands Dollar (KYD)
- South African Rand (ZAR)
- Swiss Franc (CHF)
- Euro (EUR)
- United States Dollar (USD)
*/
function formatMoney(amount){
const amountFormat = new Intl.NumberFormat("en-US",{
style: "currency",
currency: "EUR"
});
return amountFormat.format(amount);
}
const myMoney = formatMoney(2500);
console.log(myMoney);
// Output: €2,500.00
xxxxxxxxxx
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 2
})
formatter.format(1000) // "$1,000.00"
formatter.format(10) // "$10.00"
formatter.format(123233000) // "$123,233,000.00"
xxxxxxxxxx
const price = 143450;
console.log(new Intl.NumberFormat('en-US').format(price)); // 143,450
console.log(new Intl.NumberFormat('en-IN').format(price)); // 1,43,450
console.log(new Intl.NumberFormat('en-DE').format(price)); // 143.450
xxxxxxxxxx
// Create our number formatter.
var formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
// These options are needed to round to whole numbers if that's what you want.
//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)
});
formatter.format(2500); /* $2,500.00 */
xxxxxxxxxx
(1234567.8).toFixed(2).replace(/(\d)(?=(\d{2})+\d\.)/g, '$1,') // "12,34,567.80"
xxxxxxxxxx
const yourNumber = -100
const nf = new Intl.NumberFormat('en-GB', { style: 'currency', currency: 'GBP' })
console.log(nf.format(yourNumber))
Run code snippet