JavaScript Math.round()
ตัวอย่าง 1
let x = Math.round(2.5);
ความหมายและการใช้งาน
Math.round() วิธีการปัดเศษตัวเลขให้เป็นจำนวนเต็มที่ใกล้เคียงที่สุด
หมายเหตุ: 2.49 จะถูกปัดเศษลง (2) และ 2.5 จะถูกปัดขึ้น (3)
ตัวอย่าง 2
ปัดเศษตัวเลขต่าง ๆ ให้เป็นจำนวนเต็มที่ใกล้เคียงที่สุด:
let a = Math.round(2.60);
let b = Math.round(2.50);
let c = Math.round(2.49);
let d = Math.round(-2.60);
let e = Math.round(-2.50);
let f = Math.round(-2.49);
รองรับเบราว์เซอร์
Math.round()
เป็นคุณลักษณะ ECMAScript1 (ES1)
ES1 (JavaScript 1997) ได้รับการสนับสนุนอย่างสมบูรณ์ในเบราว์เซอร์ทั้งหมด:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | Yes | Yes | Yes | Yes | Yes |
ไวยากรณ์
Math.round(x)
พารามิเตอร์
Parameter | Description |
---|---|
x | Required. The number to be rounded |
คืนมูลค่า
ตัวเลขที่แสดงจำนวนเต็มที่ใกล้เคียงที่สุด |