直接使用金錢的格式化,預設會多出小數點以下2位數,用起來怪怪的。
最佳解法:
const price = 14340;
console.log(new Intl.NumberFormat().format(price)); // 14,340
會有問題的解法:
const formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
const formattedNumber = formatter.format(4999.99); // Formats as USD currency: "$4,999.99"
Intl.NumberFormat
is ideal for internationalization, providing comprehensive control over number formatting.
相關網頁
Intl.NumberFormat
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat