Javascript làm tròn 2 số thập phân

Sử dụng hàm sau với số lượng khoảng 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 chọn được đưa vào đầu

/**
 * Round half up ['round half towards positive infinity']
 * Uses exponential notation to avoid floating-point issues.
 * Negative numbers round differently than positive numbers.
 */
function round[num, decimalPlaces] {
    num = Math.round[num + "e" + decimalPlaces];
    return Number[num + "e" + -decimalPlaces];
}

// test rounding of half
console.log[ round[0.5, 0] ];  // 1
console.log[ round[-0.5, 0] ]; // 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
8 trong vòng này có thể giới thiệu khó phát hiện lỗi trong mã máy khách

function naiveRound[num, decimalPlaces] {
    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]

Triển khai tốt hơn

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. ký hiệu hàm mũ, số dương được làm tròn như mong đợi. Tuy nhiên, hãy lưu ý rằng số âm là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 "một nửa tròn" theo quy tắc, bạn sẽ thấy rằng

/**
 * Round half up ['round half towards positive infinity']
 * Uses exponential notation to avoid floating-point issues.
 * Negative numbers round differently than positive numbers.
 */
function round[num, decimalPlaces] {
    num = Math.round[num + "e" + decimalPlaces];
    return Number[num + "e" + -decimalPlaces];
}

// test rounding of half
console.log[ round[0.5, 0] ];  // 1
console.log[ round[-0.5, 0] ]; // 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
0 đánh giá thành
/**
 * Round half up ['round half towards positive infinity']
 * Uses exponential notation to avoid floating-point issues.
 * Negative numbers round differently than positive numbers.
 */
function round[num, decimalPlaces] {
    num = Math.round[num + "e" + decimalPlaces];
    return Number[num + "e" + -decimalPlaces];
}

// test rounding of half
console.log[ round[0.5, 0] ];  // 1
console.log[ round[-0.5, 0] ]; // 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
1 mặc dù
/**
 * Round half up ['round half towards positive infinity']
 * Uses exponential notation to avoid floating-point issues.
 * Negative numbers round differently than positive numbers.
 */
function round[num, decimalPlaces] {
    num = Math.round[num + "e" + decimalPlaces];
    return Number[num + "e" + -decimalPlaces];
}

// test rounding of half
console.log[ round[0.5, 0] ];  // 1
console.log[ round[-0.5, 0] ]; // 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
2 đánh giá thành
/**
 * Round half up ['round half towards positive infinity']
 * Uses exponential notation to avoid floating-point issues.
 * Negative numbers round differently than positive numbers.
 */
function round[num, decimalPlaces] {
    num = Math.round[num + "e" + decimalPlaces];
    return Number[num + "e" + -decimalPlaces];
}

// test rounding of half
console.log[ round[0.5, 0] ];  // 1
console.log[ round[-0.5, 0] ]; // 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
3. Phương pháp lodash _. Vòng sử dụng kỹ thuật này.
/**
 * Round half up ['round half towards positive infinity']
 * Uses exponential notation to avoid floating-point issues.
 * Negative numbers round differently than positive numbers.
 */
function round[num, decimalPlaces] {
    num = Math.round[num + "e" + decimalPlaces];
    return Number[num + "e" + -decimalPlaces];
}

// test rounding of half
console.log[ round[0.5, 0] ];  // 1
console.log[ round[-0.5, 0] ]; // 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 động thông thường khi làm tròn các số âm, bạn sẽ cần chuyển số âm thành dương trước khi gọi toán học. 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] {
    num = Math.round[Math.abs[num] + "e" + decimalPlaces] * Math.sign[num];
    return Number[num + "e" + -decimalPlaces];
}

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. 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. 0ULP; . Điều này chuyển sang giá trị đại diện tiếp theo sau số, cách xa số

________số 8

Điều này là cần thiết để bù đắp lỗi-tắt hoàn toàn có thể xảy ra trong quá trình mã hóa các số thập phân, đặc biệt là các số có "5" ở vị trí thập phân cuối cùng, chẳng hạn như 1.005, 2. 675 và 16. 235. Trên thực tế,

/**
 * Round half up ['round half towards positive infinity']
 * Uses exponential notation to avoid floating-point issues.
 * Negative numbers round differently than positive numbers.
 */
function round[num, decimalPlaces] {
    num = Math.round[num + "e" + decimalPlaces];
    return Number[num + "e" + -decimalPlaces];
}

// test rounding of half
console.log[ round[0.5, 0] ];  // 1
console.log[ round[-0.5, 0] ]; // 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
4 trong hệ thống thập phân được mã hóa thành
/**
 * Round half up ['round half towards positive infinity']
 * Uses exponential notation to avoid floating-point issues.
 * Negative numbers round differently than positive numbers.
 */
function round[num, decimalPlaces] {
    num = Math.round[num + "e" + decimalPlaces];
    return Number[num + "e" + -decimalPlaces];
}

// test rounding of half
console.log[ round[0.5, 0] ];  // 1
console.log[ round[-0.5, 0] ]; // 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
5 trong phao nhị phân 64 bit;

Điều đáng chú ý là nhị phân tối đa

/**
 * Round half up ['round half towards positive infinity']
 * Uses exponential notation to avoid floating-point issues.
 * Negative numbers round differently than positive numbers.
 */
function round[num, decimalPlaces] {
    num = Math.round[num + "e" + decimalPlaces];
    return Number[num + "e" + -decimalPlaces];
}

// test rounding of half
console.log[ round[0.5, 0] ];  // 1
console.log[ round[-0.5, 0] ]; // 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
8 phụ thuộc vào [1] độ lớn của số và [2] epsilon máy tương đối [2^-52]

Làm cách nào để định dạng một số là thập phân để lưu trữ nó trong mysql?Làm tròn một số đến một chữ số thập phân trong JavaScript. Có nhiều nhu cầu khác nhau để làm tròn số thực và hiển thị các chữ số cụ thể sau dấu thập phân

Làm thế nào để bạn làm tròn một số trong JavaScript?

Hàm toán học [] luôn làm tròn và trả về số nguyên nhỏ hơn hoặc bằng một số nhất định

Hơn nữa, chúng tôi có các phương pháp khác nhau để đạt được mục tiêu của mình trong hướng dẫn này

  • Sử dụng phương thức tofixed [] để làm tròn một số đến 2 số thập phân, ví dụ. kết quả const = số. cố định [2]. Phương pháp cố định sẽ làm tròn và định dạng số cho 2 thập phân

Chủ Đề