how do i print currency format in javascript

笑着哭i 提交于 2019-12-08 06:45:26

问题


i have some price values to display in my page.

i am writing a function which takes the float price and returns the formatted currency val with currency code too..

like fnPrice(1001.01) should print $ 1,000.01


回答1:


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?




回答2:


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


来源:https://stackoverflow.com/questions/4022171/how-do-i-print-currency-format-in-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!