Formatting Numbers with Thousands Separators in JavaScript
Question:
Question:Question:
function numberWithCommas(x) { x = x.toString(); var pattern = /(-?\d+)(\d{3})/; while (pattern.test(x)) x = x.replace(pattern, ","); return x; }
Question:
function numberWithCommas(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); }
將數點格式化為帶有千位分隔符號的數字?例如,將數字 1234567 顯示為 "1,234,567"。
const numberWithCommas = (x) => x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); console.log(numberWithCommas(1234567.89)); // "1,234,567.89"
以上是如何在 JavaScript 中格式化帶有千位分隔符號的數字?的詳細內容。更多資訊請關注PHP中文網其他相關文章!