Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I print currency format in JavaScript

I have some price values to display in my page.

I am writing a function that takes the float price and returns the formatted currency value with currency code too.

like fnPrice(1001.01) should print $ 1,000.01.

like image 269
KoolKabin Avatar asked Dec 01 '22 04:12

KoolKabin


1 Answers

You can using code :

function formatMoney(number) {
  return number.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
}

console.log(formatMoney(10000));   // $10,000.00
console.log(formatMoney(1000000)); // $1,000,000.00

It was answered at Javascript Function to Format as Money

Or you can custom :

function formatMoney(number) {
   return '$ '+ number.toLocaleString('en-US');
}
like image 125
TungHarry Avatar answered Dec 04 '22 12:12

TungHarry