Hướng dẫn javascript cut number to 2 decimal places - javascript cắt số đến 2 chữ số thập phân

Nói chung, làm tròn thập phân được thực hiện bằng cách mở rộng: /** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.012

Thực hiện ngây thơ

Sử dụng hàm sau với số nửa chừng, bạn sẽ nhận được giá trị tròn trên như mong đợi hoặc giá trị tròn dưới đôi khi tùy thuộc vào đầu vào.

/** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.013 này trong làm tròn có thể giới thiệu khó phát hiện các lỗi trong mã máy khách.

function naiveRound(num, decimalPlaces = 0) { var p = Math.pow(10, decimalPlaces); return Math.round(num * p) / p; } console.log( naiveRound(1.245, 2) ); // 1.25 correct (rounded as expected) console.log( naiveRound(1.255, 2) ); // 1.25 incorrect (should be 1.26) // testing edge cases console.log( naiveRound(1.005, 2) ); // 1 incorrect (should be 1.01) console.log( naiveRound(2.175, 2) ); // 2.17 incorrect (should be 2.18) console.log( naiveRound(5.015, 2) ); // 5.01 incorrect (should be 5.02)

Để xác định xem một thao tác làm tròn có liên quan đến giá trị điểm giữa hay không, hàm tròn nhân lên giá trị ban đầu được làm tròn bởi 10 ** n, trong đó n là số chữ số phân số mong muốn trong giá trị trả về, sau đó xác định xem phần còn lại Phần của giá trị lớn hơn hoặc bằng 0,5. Điều này /** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.014 với các giá trị dấu phẩy động là có vấn đề do các vấn đề của định dạng điểm nổi với biểu diễn nhị phân và độ chính xác. Điều này có nghĩa là bất kỳ phần phân số nào của một số hơi nhỏ hơn 0,5 (vì mất độ chính xác) sẽ không được làm tròn lên trên.

Trong ví dụ trước, /** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.015 là giá trị trung điểm nếu được làm tròn đến hai vị trí thập phân, giá trị 5.015 * 100 thực sự là /** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.016. Bởi vì .4999999999994 nhỏ hơn 0,5, nó được làm tròn xuống 501 và cuối cùng là kết quả là 5,01.

Triển khai tốt hơn

Ký hiệu mũ

Bằng cách chuyển đổi số thành một chuỗi trong ký hiệu theo cấp số nhân, các số dương được làm tròn như mong đợi. Nhưng, hãy lưu ý rằng các số âm tròn khác với số dương.

Trên thực tế, nó thực hiện những gì về cơ bản tương đương với "nửa tròn lên" theo quy tắc, bạn sẽ thấy rằng /** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.017 đánh giá thành /** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.018 mặc dù /** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.019 đánh giá thành // Round half away from zero function round(num, decimalPlaces = 0) { if (num < 0) return -round(-num, decimalPlaces); num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } 0. Phương pháp lodash _.Round sử dụng kỹ thuật này.

/** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.01

Nếu bạn muốn hành vi thông thường khi làm tròn các số âm, bạn sẽ cần chuyển đổi số âm thành dương trước khi gọi math.Round (), sau đó chuyển đổi chúng trở lại số âm trước khi quay lại.

// Round half away from zero function round(num, decimalPlaces = 0) { if (num < 0) return -round(-num, decimalPlaces); num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); }

Làm tròn gần đúng

Để khắc phục vấn đề làm tròn được hiển thị trong ví dụ // Round half away from zero function round(num, decimalPlaces = 0) { if (num < 0) return -round(-num, decimalPlaces); num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } 1 trước đó, chúng ta có thể xác định chức năng làm tròn tùy chỉnh thực hiện thử nghiệm "gần như bằng nhau" để xác định xem giá trị phân số có đủ gần với giá trị trung điểm có thể được làm tròn điểm giữa hay không.

// round half away from zero function round(num, decimalPlaces = 0) { if (num < 0) return -round(-num, decimalPlaces); var p = Math.pow(10, decimalPlaces); var n = num * p; var f = n - Math.floor(n); var e = Number.EPSILON * n; // Determine whether this fraction is a midpoint value. return (f >= .5 - e) ? Math.ceil(n) / p : Math.floor(n) / p; } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // -1 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1.01 console.log( round(-2.175, 2) ); // -2.18 console.log( round(-5.015, 2) ); // -5.02

Number.EPSILON

Có một kỹ thuật toán học thuần túy khác nhau để thực hiện vòng tròn đến gần nhất (sử dụng "một nửa vòng so với số không"), trong đó hiệu chỉnh Epsilon được áp dụng trước khi gọi hàm làm tròn.

Đơn giản, chúng tôi thêm giá trị float nhỏ nhất có thể (= 1.0 ULP; đơn vị ở vị trí cuối cùng) vào sản phẩm trước khi làm tròn. Điều này chuyển sang giá trị phao có thể đại diện tiếp theo, cách xa số 0, do đó nó sẽ bù đắp lỗi vòng tròn nhị phân có thể xảy ra trong quá trình nhân bằng // Round half away from zero function round(num, decimalPlaces = 0) { if (num < 0) return -round(-num, decimalPlaces); num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } 2.

/** * Round half away from zero ('commercial' rounding) * Uses correction to offset floating-point inaccuracies. * Works symmetrically for positive and negative numbers. */ function round(num, decimalPlaces = 0) { var p = Math.pow(10, decimalPlaces); var n = (num * p) * (1 + Number.EPSILON); return Math.round(n) / p; } // rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // -1 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1.01 console.log( round(-2.175, 2) ); // -2.18 console.log( round(-5.015, 2) ); // -5.02

Sau khi thêm 1 ULP, giá trị 5.015 * 100 là /** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.016 sẽ được sửa thành // Round half away from zero function round(num, decimalPlaces = 0) { if (num < 0) return -round(-num, decimalPlaces); num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } 4, điều này sẽ được làm tròn lên tới 502 và cuối cùng là kết quả là 5,02.

Lưu ý rằng kích thước của một đơn vị ở vị trí cuối cùng ("ULP") được xác định bởi (1) độ lớn của số và (2) epsilon máy tương đối (2^-52). ULP tương đối lớn hơn với số lượng có cường độ lớn hơn so với số lượng với số lượng có cường độ nhỏ hơn.

Làm tròn gấp đôi

Ở đây, chúng tôi sử dụng phương thức topRecision () để loại bỏ các lỗi làm tròn điểm nổi trong các tính toán trung gian. Đơn giản, chúng tôi làm tròn đến 15 con số quan trọng để loại bỏ lỗi làm tròn ở chữ số quan trọng thứ 16. Kỹ thuật này để dự đoán kết quả đến các chữ số quan trọng cũng được sử dụng bởi chức năng tròn Php 7.PHP 7 round function.

Giá trị 5.015 * 100 là /** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.016 sẽ được làm tròn đầu tiên đến 15 chữ số quan trọng là // Round half away from zero function round(num, decimalPlaces = 0) { if (num < 0) return -round(-num, decimalPlaces); num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } 6, sau đó nó sẽ làm tròn lại đến 502 và cuối cùng là kết quả là 5,02.

// Round half away from zero function round(num, decimalPlaces = 0) { if (num < 0) return -round(-num, decimalPlaces); var p = Math.pow(10, decimalPlaces); var n = (num * p).toPrecision(15); return Math.round(n) / p; } // rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // -1 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1.01 console.log( round(-2.175, 2) ); // -2.18 console.log( round(-5.015, 2) ); // -5.02

Thư viện JavaScript chính xác tùy ý - Decimal.js - decimal.js

// Round half away from zero function round(num, decimalPlaces = 0) { return new Decimal(num).toDecimalPlaces(decimalPlaces).toNumber(); } // rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // -1 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1.01 console.log( round(-2.175, 2) ); // -2.18 console.log( round(-5.015, 2) ); // -5.02<script src="//cdnjs.cloudflare.com/ajax/libs/decimal.js/10.2.1/decimal.js" integrity="sha512-GKse2KVGCCMVBn4riigHjXE8j5hCxYLPXDw8AvcjUtrt+a9TbZFtIKGdArXwYOlZvdmkhQLWQ46ZE3Q1RIa7uQ==" crossorigin="anonymous"></script>

Giải pháp 1: Chuỗi theo ký hiệu theo cấp số nhân

Lấy cảm hứng từ giải pháp được cung cấp bởi Kfish tại đây: //stackoverflow.com/a/55521592/4208440

Một giải pháp giảm đơn giản cung cấp cách làm tròn thập phân, sàn và trần chính xác cho một số lượng số thập phân cụ thể mà không cần thêm toàn bộ thư viện. Nó xử lý phao giống như số thập phân bằng cách khắc phục các vấn đề làm tròn nhị phân để tránh kết quả không mong muốn: ví dụ, sàn ((0,1+0,7)*10) sẽ trả về kết quả dự kiến ​​8.

Các số được làm tròn đến một số chữ số phân số cụ thể. Chỉ định độ chính xác âm sẽ làm tròn đến bất kỳ số lượng địa điểm nào ở bên trái của dấu thập phân.

// Solution 1 var DecimalPrecision = (function() { if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var decimalAdjust = function myself(type, num, decimalPlaces) { if (type === 'round' && num < 0) return -myself(type, -num, decimalPlaces); var shift = function(value, exponent) { value = (value + 'e').split('e'); return +(value[0] + 'e' + (+value[1] + (exponent || 0))); }; var n = shift(num, +decimalPlaces); return shift(Math[type](n), -decimalPlaces); }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces); }, // Decimal ceil ceil: function(num, decimalPlaces) { return decimalAdjust('ceil', num, decimalPlaces); }, // Decimal floor floor: function(num, decimalPlaces) { return decimalAdjust('floor', num, decimalPlaces); }, // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); }, // Format using fixed-point notation toFixed: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); } }; })(); // test rounding of half console.log(DecimalPrecision.round(0.5)); // 1 console.log(DecimalPrecision.round(-0.5)); // -1 // testing very small numbers console.log(DecimalPrecision.ceil(1e-8, 2) === 0.01); console.log(DecimalPrecision.floor(1e-8, 2) === 0); // testing simple cases console.log(DecimalPrecision.round(5.12, 1) === 5.1); console.log(DecimalPrecision.round(-5.12, 1) === -5.1); console.log(DecimalPrecision.ceil(5.12, 1) === 5.2); console.log(DecimalPrecision.ceil(-5.12, 1) === -5.1); console.log(DecimalPrecision.floor(5.12, 1) === 5.1); console.log(DecimalPrecision.floor(-5.12, 1) === -5.2); console.log(DecimalPrecision.trunc(5.12, 1) === 5.1); console.log(DecimalPrecision.trunc(-5.12, 1) === -5.1); // testing edge cases for round console.log(DecimalPrecision.round(1.005, 2) === 1.01); console.log(DecimalPrecision.round(39.425, 2) === 39.43); console.log(DecimalPrecision.round(-1.005, 2) === -1.01); console.log(DecimalPrecision.round(-39.425, 2) === -39.43); // testing edge cases for ceil console.log(DecimalPrecision.ceil(9.13, 2) === 9.13); console.log(DecimalPrecision.ceil(65.18, 2) === 65.18); console.log(DecimalPrecision.ceil(-2.26, 2) === -2.26); console.log(DecimalPrecision.ceil(-18.15, 2) === -18.15); // testing edge cases for floor console.log(DecimalPrecision.floor(2.26, 2) === 2.26); console.log(DecimalPrecision.floor(18.15, 2) === 18.15); console.log(DecimalPrecision.floor(-9.13, 2) === -9.13); console.log(DecimalPrecision.floor(-65.18, 2) === -65.18); // testing edge cases for trunc console.log(DecimalPrecision.trunc(2.26, 2) === 2.26); console.log(DecimalPrecision.trunc(18.15, 2) === 18.15); console.log(DecimalPrecision.trunc(-2.26, 2) === -2.26); console.log(DecimalPrecision.trunc(-18.15, 2) === -18.15); // testing round to tens and hundreds console.log(DecimalPrecision.round(1262.48, -1) === 1260); console.log(DecimalPrecision.round(1262.48, -2) === 1300); // testing toFixed() console.log(DecimalPrecision.toFixed(1.005, 2) === "1.01");

Giải pháp 2: Toán học thuần túy (số.epsilon)

Giải pháp này tránh bất kỳ chuyển đổi / thao tác chuỗi nào vì bất kỳ lý do hiệu suất nào.

// Solution 2 var DecimalPrecision2 = (function() { if (Number.EPSILON === undefined) { Number.EPSILON = Math.pow(2, -52); } if (Math.trunc === undefined) { Math.trunc = function(v) { return v < 0 ? Math.ceil(v) : Math.floor(v); }; } var powers = [ 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15, 1e16, 1e17, 1e18, 1e19, 1e20, 1e21, 1e22 ]; var intpow10 = function(power) { if (power < 0 || power > 22) { return Math.pow(10, power); } return powers[power]; }; var isRound = function(num, decimalPlaces) { //return decimalPlaces >= 0 && // +num.toFixed(decimalPlaces) === num; var p = intpow10(decimalPlaces); return Math.round(num * p) / p === num; }; var decimalAdjust = function(type, num, decimalPlaces) { if (type !== 'round' && isRound(num, decimalPlaces || 0)) return num; var p = intpow10(decimalPlaces || 0); var n = (num * p) * (1 + Number.EPSILON); return Math[type](n) / p; }; return { // Decimal round (half away from zero) round: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces); }, // Decimal ceil ceil: function(num, decimalPlaces) { return decimalAdjust('ceil', num, decimalPlaces); }, // Decimal floor floor: function(num, decimalPlaces) { return decimalAdjust('floor', num, decimalPlaces); }, // Decimal trunc trunc: function(num, decimalPlaces) { return decimalAdjust('trunc', num, decimalPlaces); }, // Format using fixed-point notation toFixed: function(num, decimalPlaces) { return decimalAdjust('round', num, decimalPlaces).toFixed(decimalPlaces); } }; })(); // test rounding of half console.log(DecimalPrecision2.round(0.5)); // 1 console.log(DecimalPrecision2.round(-0.5)); // -1 // testing very small numbers console.log(DecimalPrecision2.ceil(1e-8, 2) === 0.01); console.log(DecimalPrecision2.floor(1e-8, 2) === 0); // testing simple cases console.log(DecimalPrecision2.round(5.12, 1) === 5.1); console.log(DecimalPrecision2.round(-5.12, 1) === -5.1); console.log(DecimalPrecision2.ceil(5.12, 1) === 5.2); console.log(DecimalPrecision2.ceil(-5.12, 1) === -5.1); console.log(DecimalPrecision2.floor(5.12, 1) === 5.1); console.log(DecimalPrecision2.floor(-5.12, 1) === -5.2); console.log(DecimalPrecision2.trunc(5.12, 1) === 5.1); console.log(DecimalPrecision2.trunc(-5.12, 1) === -5.1); // testing edge cases for round console.log(DecimalPrecision2.round(1.005, 2) === 1.01); console.log(DecimalPrecision2.round(39.425, 2) === 39.43); console.log(DecimalPrecision2.round(-1.005, 2) === -1.01); console.log(DecimalPrecision2.round(-39.425, 2) === -39.43); // testing edge cases for ceil console.log(DecimalPrecision2.ceil(9.13, 2) === 9.13); console.log(DecimalPrecision2.ceil(65.18, 2) === 65.18); console.log(DecimalPrecision2.ceil(-2.26, 2) === -2.26); console.log(DecimalPrecision2.ceil(-18.15, 2) === -18.15); // testing edge cases for floor console.log(DecimalPrecision2.floor(2.26, 2) === 2.26); console.log(DecimalPrecision2.floor(18.15, 2) === 18.15); console.log(DecimalPrecision2.floor(-9.13, 2) === -9.13); console.log(DecimalPrecision2.floor(-65.18, 2) === -65.18); // testing edge cases for trunc console.log(DecimalPrecision2.trunc(2.26, 2) === 2.26); console.log(DecimalPrecision2.trunc(18.15, 2) === 18.15); console.log(DecimalPrecision2.trunc(-2.26, 2) === -2.26); console.log(DecimalPrecision2.trunc(-18.15, 2) === -18.15); // testing round to tens and hundreds console.log(DecimalPrecision2.round(1262.48, -1) === 1260); console.log(DecimalPrecision2.round(1262.48, -2) === 1300); // testing toFixed() console.log(DecimalPrecision2.toFixed(1.005, 2) === "1.01");

Giải pháp 3: Làm tròn đôi

Giải pháp này sử dụng phương thức topRecision () để loại bỏ các lỗi làm tròn điểm nổi.

/** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.010

Giải pháp 4: Nhân đôi V2

Giải pháp này giống như Giải pháp 3, tuy nhiên nó sử dụng hàm // Round half away from zero function round(num, decimalPlaces = 0) { if (num < 0) return -round(-num, decimalPlaces); num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } 7 tùy chỉnh.

/** * Round half up ('round half towards positive infinity') * Negative numbers round differently than positive numbers. */ function round(num, decimalPlaces = 0) { num = Math.round(num + "e" + decimalPlaces); return Number(num + "e" + -decimalPlaces); } // test rounding of half console.log( round(0.5) ); // 1 console.log( round(-0.5) ); // 0 // testing edge cases console.log( round(1.005, 2) ); // 1.01 console.log( round(2.175, 2) ); // 2.18 console.log( round(5.015, 2) ); // 5.02 console.log( round(-1.005, 2) ); // -1 console.log( round(-2.175, 2) ); // -2.17 console.log( round(-5.015, 2) ); // -5.011

Điểm chuẩn

//jsbench.github.io/#31ec3a8b3d22bd840f8e6822e681a3ac

Dưới đây là một điểm chuẩn so sánh các hoạt động mỗi giây trong các giải pháp trên trên Chrome 85.0.4183.83.Rõ ràng tất cả các trình duyệt khác nhau, vì vậy số dặm của bạn có thể khác nhau.

(Lưu ý: nhiều hơn là tốt hơn)

Cảm ơn @Mike vì đã thêm ảnh chụp màn hình của điểm chuẩn.

Chủ đề