JavaScript วัตถุคณิตศาสตร์
ออบเจ็กต์ JavaScript Math ช่วยให้คุณทำงานทางคณิตศาสตร์กับตัวเลขได้
ตัวอย่าง
Math.PI;
วัตถุคณิตศาสตร์
ไม่เหมือนกับวัตถุอื่น ๆ วัตถุคณิตศาสตร์ไม่มีตัวสร้าง
วัตถุคณิตศาสตร์เป็นแบบคงที่
สามารถใช้วิธีการและคุณสมบัติทั้งหมดได้โดยไม่ต้องสร้างวัตถุคณิตศาสตร์ก่อน
คุณสมบัติทางคณิตศาสตร์ (ค่าคงที่)
ไวยากรณ์สำหรับคุณสมบัติ Math คือ:Math.property
JavaScript ให้ค่าคงที่ทางคณิตศาสตร์ 8 ค่าที่สามารถเข้าถึงได้เป็นคุณสมบัติทางคณิตศาสตร์:
ตัวอย่าง
Math.E // returns Euler's number
Math.PI // returns PI
Math.SQRT2 // returns the square root of 2
Math.SQRT1_2 // returns the square root of 1/2
Math.LN2 // returns the natural logarithm of 2
Math.LN10 // returns the natural logarithm of 10
Math.LOG2E // returns base 2 logarithm of E
Math.LOG10E // returns base 10 logarithm of E
วิธีคณิตศาสตร์
ไวยากรณ์สำหรับวิธีใด ๆ ทางคณิตศาสตร์คือ: Math.method(number)
ตัวเลขเป็นจำนวนเต็ม
มีวิธีทั่วไป 4 วิธีในการปัดเศษตัวเลขให้เป็นจำนวนเต็ม:
คณิตศาสตร์ รอบ(x) | ส่งกลับค่า x ที่ปัดเศษเป็นจำนวนเต็มที่ใกล้ที่สุด |
Math.ceil(x) | ส่งกลับ x ที่ปัดเศษขึ้นเป็นจำนวนเต็มที่ใกล้เคียงที่สุด |
คณิตศาสตร์ชั้น(x) | ส่งกลับ x ที่ปัดเศษลงเป็นจำนวนเต็มที่ใกล้เคียงที่สุด |
Math.trunc(x) | ส่งกลับส่วนจำนวนเต็มของ x ( ใหม่ใน ES6 ) |
คณิตศาสตร์.รอบ()
Math.round(x)
ส่งกลับจำนวนเต็มที่ใกล้เคียงที่สุด:
ตัวอย่าง
Math.round(4.6);
Math.round(4.5);
Math.round(4.4);
Math.ceil()
Math.ceil(x)
ส่งคืนค่าของ x ที่ปัดเศษขึ้นเป็นจำนวนเต็มที่ใกล้เคียงที่สุด:
ตัวอย่าง
Math.ceil(4.9);
Math.ceil(4.7);
Math.ceil(4.4);
Math.ceil(4.2);
Math.ceil(-4.2);
คณิตศาสตร์ชั้น()
Math.floor(x)
ส่งคืนค่าของ x ที่ปัดเศษลงเป็นจำนวนเต็มที่ใกล้เคียงที่สุด:
ตัวอย่าง
Math.floor(4.9);
Math.floor(4.7);
Math.floor(4.4);
Math.floor(4.2);
Math.floor(-4.2);
Math.trunc()
Math.trunc(x)
ส่งคืนส่วนจำนวนเต็มของ x:
ตัวอย่าง
Math.trunc(4.9);
Math.trunc(4.7);
Math.trunc(4.4);
Math.trunc(4.2);
Math.trunc(-4.2);
Math.sign()
Math.sign(x)
คืนค่าถ้า x เป็นค่าลบ, null หรือค่าบวก:
ตัวอย่าง
Math.sign(-4);
Math.sign(0);
Math.sign(4);
เพิ่ม Math.trunc() และ Math.sign() ใน JavaScript 2015 - ES6
Math.pow()
Math.pow(x, y)
ส่งคืนค่า x ยกกำลัง y:
ตัวอย่าง
Math.pow(8, 2);
คณิตศาสตร์.sqrt()
Math.sqrt(x)
คืนค่ารากที่สองของ x:
ตัวอย่าง
Math.sqrt(64);
Math.abs()
Math.abs(x)
ส่งคืนค่าสัมบูรณ์ (บวก) ของ x:
ตัวอย่าง
Math.abs(-4.7);
คณิต.สิน ()
Math.sin(x)
ส่งกลับค่าไซน์ (ค่าระหว่าง -1 ถึง 1) ของมุม x (กำหนดเป็นเรเดียน)
หากคุณต้องการใช้องศาแทนเรเดียน คุณต้องแปลงองศาเป็นเรเดียน:
มุมเป็นเรเดียน = มุมเป็นองศา x PI / 180
ตัวอย่าง
Math.sin(90 * Math.PI / 180); // returns 1 (the sine of 90 degrees)
คณิตศาสตร์.cos()
Math.cos(x)
ส่งกลับค่าโคไซน์ (ค่าระหว่าง -1 ถึง 1) ของมุม x (กำหนดเป็นเรเดียน)
หากคุณต้องการใช้องศาแทนเรเดียน คุณต้องแปลงองศาเป็นเรเดียน:
มุมเป็นเรเดียน = มุมเป็นองศา x PI / 180
ตัวอย่าง
Math.cos(0 * Math.PI / 180); // returns 1 (the cos of 0 degrees)
Math.min() และ Math.max()
Math.min()
และMath.max()
สามารถใช้เพื่อค้นหาค่าต่ำสุดหรือสูงสุดในรายการอาร์กิวเมนต์:
ตัวอย่าง
Math.min(0, 150, 30, 20, -8, -200);
ตัวอย่าง
Math.max(0, 150, 30, 20, -8, -200);
คณิตศาสตร์สุ่ม()
Math.random()
ส่งกลับตัวเลขสุ่มระหว่าง 0 (รวม) และ 1 (ไม่รวม):
ตัวอย่าง
Math.random();
คุณจะได้เรียนรู้เพิ่มเติมเกี่ยวกับMath.random()
บทต่อไปของบทช่วยสอนนี้
Math.log() Method
Math.log(x)
ส่งกลับค่าลอการิทึมธรรมชาติของ x
ลอการิทึมธรรมชาติส่งคืนเวลาที่จำเป็นเพื่อให้ถึงระดับการเติบโตที่แน่นอน:
ตัวอย่าง
Math.log(1);
Math.log(2);
Math.log(3);
Math.E และ Math.log() เป็นฝาแฝด
เราต้องคูณ Math.E กี่ครั้งเพื่อให้ได้ 10?
Math.log(10);
วิธี Math.log2()
Math.log2(x)
ส่งกลับค่าลอการิทึมฐาน 2 ของ x
เราต้องคูณ 2 กี่ครั้งเพื่อให้ได้ 8?
Math.log2(8);
วิธี Math.log10()
Math.log10(x)
ส่งกลับค่าลอการิทึมฐาน 10 ของ x
เราต้องคูณ 10 กี่ครั้งจึงจะได้ 1000?
Math.log10(1000);
วิธีการทางคณิตศาสตร์ของจาวาสคริปต์
Method | Description |
---|---|
abs(x) | Returns the absolute value of x |
acos(x) | Returns the arccosine of x, in radians |
acosh(x) | Returns the hyperbolic arccosine of x |
asin(x) | Returns the arcsine of x, in radians |
asinh(x) | Returns the hyperbolic arcsine of x |
atan(x) | Returns the arctangent of x as a numeric value between -PI/2 and PI/2 radians |
atan2(y, x) | Returns the arctangent of the quotient of its arguments |
atanh(x) | Returns the hyperbolic arctangent of x |
cbrt(x) | Returns the cubic root of x |
ceil(x) | Returns x, rounded upwards to the nearest integer |
cos(x) | Returns the cosine of x (x is in radians) |
cosh(x) | Returns the hyperbolic cosine of x |
exp(x) | Returns the value of Ex |
floor(x) | Returns x, rounded downwards to the nearest integer |
log(x) | Returns the natural logarithm (base E) of x |
max(x, y, z, ..., n) | Returns the number with the highest value |
min(x, y, z, ..., n) | Returns the number with the lowest value |
pow(x, y) | Returns the value of x to the power of y |
random() | Returns a random number between 0 and 1 |
round(x) | Rounds x to the nearest integer |
sign(x) | Returns if x is negative, null or positive (-1, 0, 1) |
sin(x) | Returns the sine of x (x is in radians) |
sinh(x) | Returns the hyperbolic sine of x |
sqrt(x) | Returns the square root of x |
tan(x) | Returns the tangent of an angle |
tanh(x) | Returns the hyperbolic tangent of a number |
trunc(x) | Returns the integer part of a number (x) |
อ้างอิงคณิตศาสตร์ที่สมบูรณ์
สำหรับข้อมูลอ้างอิงที่สมบูรณ์ ไปที่Complete Math Object Referenceของเรา
ข้อมูลอ้างอิงประกอบด้วยคำอธิบายและตัวอย่างของคุณสมบัติและวิธีการทางคณิตศาสตร์ทั้งหมด