开发者

How do I print currency format in JavaScript

开发者 https://www.devze.com 2023-01-21 19:26 出处:网络
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.

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.


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');
}


You've got to do this by hand, there is nothing builtin into JS. For an example look at this post here: How can I format numbers as money in JavaScript?

0

精彩评论

暂无评论...
验证码 换一张
取 消