Hướng dẫn number currency in javascript - tiền tệ số trong javascript
Nội dung bài viết Intl.NumberFormat mặc dù được hỗ trợ trong những brower hiện đại để Javascript format giá tiền nhưng nếu một lúc nào đó devjs muốn viết cho bản thân mình or cho team một chức năng tương tự thì làm như thế nào? Bài dưới đây sẽ có những phương án rất đơn giản mà hiệu quả Javascript format giá tiền. Đọc thêm tips javascript Tips - Math.random() in JavaScript Tipjs - 5 cách chuyển value sang string trong javascript Ví dụ: 12345.67120398 trở thành 12,345.67. Chúng ta có thể làm đơn giản Kiểu tiền tệ trong JavaScript như ví dụ dưới đây. Chúng ta có thể làm tương tự khi sử dụng function format1(n, currency) { return currency + n.toFixed(2).replace(/./g, function(c, i, a) { return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c; }); } function format2(n, currency) { return currency + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'); } var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67]; for (var i = 0; i < numbers.length; i++) { console.log(format1(numbers[i], '£ ')); } for (var i = 0; i < numbers.length; i++) { console.log(format2(numbers[i], 'vnd ')); } Kết qủa Format #1: £ 1.00 £ 12.00 £ 123.00 £ 1,234.00 £ 12,345.00 £ 123,456.00 £ 1,234,567.00 £ 12,345.67 Format #2: vnd 1.00 vnd 12.00 vnd 123.00 vnd 1,234.00 vnd 12,345.00 vnd 123,456.00 vnd 1,234,567.00 vnd 12,345.67 Hàm number_format trong JavaScriptCó một chức năng ECMAScript mới hơn được cung cấp bởi Internationalization API const formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2 }) // "$1,000.00" formatter.format(1000); // "$10.00" formatter.format(10); // "$1,234,567,890.00" formatter.format(1234567890); Đọc thêm tips javascript Tips - Math.random() in JavaScript Tipjs - 5 cách chuyển value sang string trong javascript Javascript format giá tiền với toFixed()const test1 = '1234567890' const format = test1.replace(/\B(?=(\d{3})+(?!\d))/g, ',') console.log(format) // 1,234,567,890 Ví dụ: 12345.67120398 trở thành 12,345.67. Chúng ta có thể làm đơn giản Kiểu tiền tệ trong JavaScript như ví dụ dưới đây.function formatCash(str) { return str.split('').reverse().reduce((prev, next, index) => { return ((index % 3) ? next : (next + ',')) + prev }) } console.log(formatCash('1234567890')) // 1,234,567,890 Format VND JavaScript » Code Đã đăng vào thg 5 4, 2020 7:15 SA 0 phút đọc Định dạng tiền tệ trong JavaScript Xem thêm. QABug chúc các bạn thực hiện Format money javascript thành công. Format VND JavaScript » Code Đã đăng vào thg 5 4, 2020 7:15 SA 0 phút đọc Có một chức năng ECMAScript mới hơn được cung cấp bởi Internationalization API javascript format giá tiền const number = 123456.789; console.log(new Intl.NumberFormat('vi-VN', { style: 'currency', currency: 'VND' }).format(number)); // expected output: "123.457 ₫" console.log(new Intl.NumberFormat('it-IT', { style: 'currency', currency: 'VND' }).format(number)); // expected output: "123.457 VND" // the Japanese yen doesn't use a minor unit console.log(new Intl.NumberFormat('ja-JP', { style: 'currency', currency: 'JPY' }).format(number)); // expected output: "¥123,457" // limit to three significant digits console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number)); // expected output: "1,23,000" Format VND JavaScript Đã đăng vào thg 5 4, 2020 7:15 SA 0 phút đọc Intl.NumberFormat mặc dù được hỗ trợ trong những brower hiện đại để Javascript format giá tiền nhưng nếu một lúc nào đó devjs muốn viết cho bản thân mình or cho team một chức năng tương tự thì làm như thế nào? Bài dưới đây sẽ có những phương án rất đơn giản mà hiệu quả Javascript format giá tiền.Kết qủa Đọc thêm tips javascript Tips - Math.random() in JavaScript Tipjs - 5 cách chuyển value sang string trong javascript Javascript format giá tiền với toFixed()Ví dụ: 12345.67120398 trở thành 12,345.67. Chúng ta có thể làm đơn giản Kiểu tiền tệ trong JavaScript như ví dụ dưới đây. var yourBalance = 2489.8237; //returns 2489.824 (rounds up) yourBalance.toFixed(3); //returns 2489.82 yourBalance.toFixed(2); //returns 2489.8237000 (pads the decimals) yourBalance.toFixed(7); Chúng ta có thể làm tương tự khi sử dụng function format1(n, currency) { return currency + n.toFixed(2).replace(/./g, function(c, i, a) { return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c; }); } function format2(n, currency) { return currency + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'); } var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67]; for (var i = 0; i < numbers.length; i++) { console.log(format1(numbers[i], '£ ')); } for (var i = 0; i < numbers.length; i++) { console.log(format2(numbers[i], 'vnd ')); }8, như demo dưới đây. function format1(n, currency) { return currency + n.toFixed(2).replace(/./g, function(c, i, a) { return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c; }); } function format2(n, currency) { return currency + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'); } var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67]; for (var i = 0; i < numbers.length; i++) { console.log(format1(numbers[i], '£ ')); } for (var i = 0; i < numbers.length; i++) { console.log(format2(numbers[i], 'vnd ')); } Kết qủa Format #1: £ 1.00 £ 12.00 £ 123.00 £ 1,234.00 £ 12,345.00 £ 123,456.00 £ 1,234,567.00 £ 12,345.67 Format #2: vnd 1.00 vnd 12.00 vnd 123.00 vnd 1,234.00 vnd 12,345.00 vnd 123,456.00 vnd 1,234,567.00 vnd 12,345.67 Hàm number_format trong JavaScriptCó một chức năng ECMAScript mới hơn được cung cấp bởi Internationalization API const formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2 }) // "$1,000.00" formatter.format(1000); // "$10.00" formatter.format(10); // "$1,234,567,890.00" formatter.format(1234567890); Đọc thêm tips javascript Tips - Math.random() in JavaScript Tipjs - 5 cách chuyển value sang string trong javascript Javascript format giá tiền với toFixed()const test1 = '1234567890' const format = test1.replace(/\B(?=(\d{3})+(?!\d))/g, ',') console.log(format) // 1,234,567,890 Ví dụ: 12345.67120398 trở thành 12,345.67. Chúng ta có thể làm đơn giản Kiểu tiền tệ trong JavaScript như ví dụ dưới đây.function formatCash(str) { return str.split('').reverse().reduce((prev, next, index) => { return ((index % 3) ? next : (next + ',')) + prev }) } console.log(formatCash('1234567890')) // 1,234,567,890 Chúng ta có thể làm tương tự khi sử dụng function format1(n, currency) { return currency + n.toFixed(2).replace(/./g, function(c, i, a) { return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c; }); } function format2(n, currency) { return currency + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'); } var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67]; for (var i = 0; i < numbers.length; i++) { console.log(format1(numbers[i], '£ ')); } for (var i = 0; i < numbers.length; i++) { console.log(format2(numbers[i], 'vnd ')); }8, như demo dưới đây. 0 phút đọc Kết qủa Javascript format giá tiền với toFixed()Ví dụ: 12345.67120398 trở thành 12,345.67. Chúng ta có thể làm đơn giản Kiểu tiền tệ trong JavaScript như ví dụ dưới đây. function format1(n, currency) { return currency + n.toFixed(2).replace(/./g, function(c, i, a) { return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c; }); } function format2(n, currency) { return currency + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'); } var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67]; for (var i = 0; i < numbers.length; i++) { console.log(format1(numbers[i], '£ ')); } for (var i = 0; i < numbers.length; i++) { console.log(format2(numbers[i], 'vnd ')); }4 Chúng ta có thể làm tương tự khi sử dụng function format1(n, currency) { return currency + n.toFixed(2).replace(/./g, function(c, i, a) { return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c; }); } function format2(n, currency) { return currency + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'); } var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67]; for (var i = 0; i < numbers.length; i++) { console.log(format1(numbers[i], '£ ')); } for (var i = 0; i < numbers.length; i++) { console.log(format2(numbers[i], 'vnd ')); }8, như demo dưới đây.Kết qủareplace, như demo dưới đây. function format1(n, currency) { return currency + n.toFixed(2).replace(/./g, function(c, i, a) { return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c; }); } function format2(n, currency) { return currency + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'); } var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67]; for (var i = 0; i < numbers.length; i++) { console.log(format1(numbers[i], '£ ')); } for (var i = 0; i < numbers.length; i++) { console.log(format2(numbers[i], 'vnd ')); }5
Format #1: £ 1.00 £ 12.00 £ 123.00 £ 1,234.00 £ 12,345.00 £ 123,456.00 £ 1,234,567.00 £ 12,345.67 Format #2: vnd 1.00 vnd 12.00 vnd 123.00 vnd 1,234.00 vnd 12,345.00 vnd 123,456.00 vnd 1,234,567.00 vnd 12,345.67 function format1(n, currency) { return currency + n.toFixed(2).replace(/./g, function(c, i, a) { return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c; }); } function format2(n, currency) { return currency + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'); } var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67]; for (var i = 0; i < numbers.length; i++) { console.log(format1(numbers[i], '£ ')); } for (var i = 0; i < numbers.length; i++) { console.log(format2(numbers[i], 'vnd ')); }6 Hàm number_format trong JavaScriptCó một chức năng ECMAScript mới hơn được cung cấp bởi Internationalization API function format1(n, currency) { return currency + n.toFixed(2).replace(/./g, function(c, i, a) { return i > 0 && c !== "." && (a.length - i) % 3 === 0 ? "," + c : c; }); } function format2(n, currency) { return currency + n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,'); } var numbers = [1, 12, 123, 1234, 12345, 123456, 1234567, 12345.67]; for (var i = 0; i < numbers.length; i++) { console.log(format1(numbers[i], '£ ')); } for (var i = 0; i < numbers.length; i++) { console.log(format2(numbers[i], 'vnd ')); }7 javascript format giá tiền |