072_Math对象
1. Math對象允許您對數字執行數學任務。
2. Math屬性(常量)
2.1. JavaScript提供了可由Math對象訪問的8個數學常量:
Math.E // 返回算術常量e, 即自然對數的底數, 其值近似于2.71828 Math.PI // 返回圓周率(PI)(約等于3.14159) Math.SQRT2 // 返回2的平方根(約等于1.414) Math.SQRT1_2 // 返回1/2的平方根(約等于0.707) Math.LN2 // 返回2的自然對數(約等于0.693) Math.LN10 // 返回10的自然對數(約等于2.302) Math.LOG2E // 返回以2為底的e的對數(約等于1.44) Math.LOG10E // 返回以10為底的e的對數(約等于0.434)2.2. 例子
2.2.1. 代碼
<!DOCTYPE html> <html lang="zh-cn"><head><meta charset="utf-8" /><title>Math對象常量</title></head><body><script type="text/javascript">document.write('Math.E = ' + Math.E + '<br />'); // 返回算術常量e, 即自然對數的底數, 其值近似于2.71828document.write('Math.PI = ' + Math.PI + '<br />'); // 返回圓周率(PI)(約等于3.14159)document.write('Math.SQRT2 = ' + Math.SQRT2 + '<br />'); // 返回2的平方根(約等于1.414)document.write('Math.SQRT1_2 = ' + Math.SQRT1_2 + '<br />'); // 返回1/2的平方根(約等于0.707)document.write('Math.LN2 = ' + Math.LN2 + '<br />'); // 返回2的自然對數(約等于0.693)document.write('Math.LN10 = ' + Math.LN10 + '<br />'); // 返回10的自然對數(約等于2.302)document.write('Math.LOG2E = ' + Math.LOG2E + '<br />'); // 返回以2為底的e的對數(約等于1.44)document.write('Math.LOG10E = ' + Math.LOG10E + '<br />'); // 返回以10為底的e的對數(約等于0.434)</script></body> </html>2.2.2. 效果圖
3. Math.min()和Math.max()
3.1 Math.min()和Math.max()可用于查找參數列表中的最低或最高值。
3.2. 例子
3.2.1. 代碼
<!DOCTYPE html> <html lang="zh-cn"><head><meta charset="utf-8" /><title>Math對象最大值和最小值</title></head><body><script type="text/javascript">document.write('Math.min = ' + Math.min(0, 450, 35, 10, -8, -300, -78) + '<br />');document.write('Math.max = ' + Math.max(0, 450, 35, 10, -8, -300, -78) + '<br />');</script></body> </html>3.2.2. 效果圖
4. Math.round()、Math.ceil()和Math.floor()舍入
4.1. Math.round(x)的返回值是x四舍五入為最接近的整數。
4.2. Math.ceil(x)的返回值是x上舍入最接近的整數。
4.3. Math.floor(x)的返回值是x下舍入最接近的整數。
4.4. 例子
4.4.1. 代碼
<!DOCTYPE html> <html lang="zh-cn"><head><meta charset="utf-8" /><title>Math.round()、Math.ceil()和Math.floor()舍入</title></head><body><script type="text/javascript">var a = 3.9, b = 5.2;document.write('Math.round(' + a + ') = ' + Math.round(a) + '<br />');document.write('Math.round(' + b + ') = ' + Math.round(b) + '<br />');document.write('Math.ceil(' + a + ') = ' + Math.ceil(a) + '<br />');document.write('Math.ceil(' + b + ') = ' + Math.ceil(b) + '<br />');document.write('Math.floor(' + a + ') = ' + Math.floor(a) + '<br />');document.write('Math.floor(' + b + ') = ' + Math.floor(b) + '<br />');</script></body> </html>4.4.2. 效果圖
5. Math.pow()
5.1. Math.pow(x, y)的返回值是x的y次冪。
5.2. 實例
Math.pow(8, 2); // 返回 646. Math.sqrt()
6.1. Math.sqrt(x)返回x的平方根。
6.2. 實例
Math.sqrt(64); // 返回 87. Math.abs()
7.1. Math.abs(x)返回x的絕對(正)值。
7.2. 實例
Math.abs(-4.7); // 返回 4.77.3. 例子
7.3.1. 代碼
<!DOCTYPE html> <html lang="zh-cn"><head><meta charset="utf-8" /><title>Math.pow()、Math.sqrt()和Math.abs()</title></head><body><script type="text/javascript">document.write('Math.pow(8, 2) = ' + Math.pow(8, 2) + '<br />');document.write('Math.sqrt(64) = ' + Math.sqrt(64) + '<br />');document.write('Math.abs(-4.7) = ' + Math.abs(-4.7) + '<br />');</script></body> </html>7.3.2. 效果圖
8. Math.random()
8.1. Math.random()返回介于0(包括)與1(不包括)之間的隨機數。
8.2. Math.random() 總是返回小于1的數。
8.3. 返回介于min(包括)和max(不包括)之間的隨機數:
function getRndInteger(min, max) {return Math.floor(Math.random() * (max - min) ) + min; }8.4. 返回介于min(包括)和max(包括)之間的隨機數:
function getRndInteger(min, max) {return Math.floor(Math.random() * (max - min + 1) ) + min; }8.5. 返回介于min(不包括)和max(包括)之間的隨機數:
function getRndInteger(min, max) {return Math.floor(Math.random() * (max - min) ) + min + 1; }8.6. 例子
8.6.1. 代碼
<!DOCTYPE html> <html lang="zh-cn"><head><meta charset="utf-8" /><title>Math.random()隨機數</title></head><body><script type="text/javascript">document.write('Math.random() = ' + Math.random() + '<br /><hr />');document.write('<p>返回 0 至 9 之間的數</p>');document.write('Math.floor(Math.random() * 10) = ' + Math.floor(Math.random() * 10) + '<br /><hr />');document.write('<p>返回 0 至 10 之間的數</p>');document.write('Math.floor(Math.random() * 11) = ' + Math.floor(Math.random() * 11) + '<br />');document.write('Math.ceil(Math.random() * 10) = ' + Math.ceil(Math.random() * 10) + '<br /><hr />');document.write('<p>返回 1 至 10 之間的數</p>');document.write('Math.floor(Math.random() * 10) + 1 = ' + (Math.floor(Math.random() * 10) + 1) + '<br /><hr />');function getRndInteger1(min, max) {return Math.floor(Math.random() * (max - min) ) + min;}function getRndInteger2(min, max) {return Math.floor(Math.random() * (max - min + 1) ) + min;}function getRndInteger3(min, max) {return Math.floor(Math.random() * (max - min) ) + min + 1;}document.write('<p>返回 0 至 99 之間的數</p>');document.write('getRndInteger1(0, 100) = ' + getRndInteger1(0, 100) + '<br /><hr />');document.write('<p>返回 0 至 100 之間的數</p>');document.write('getRndInteger2(0, 100) = ' + getRndInteger2(0, 100) + '<br /><hr />');document.write('<p>返回 1 至 100 之間的數</p>');document.write('getRndInteger3(0, 100) = ' + getRndInteger3(0, 100) + '<br />');</script></body> </html>8.6.2. 效果圖
9. 三角函數
9.1. 根號2約等于1.414。
9.2. 根號3約等于1.732。
9.3. 弧度值2PI / 360。
9.4. 特殊角的三角函數值表
9.5. Math.sin(x)返回角x(以弧度計)的正弦(介于-1與1之間的值)。
9.6. Math.cos(x)返回角x(以弧度計)的余弦(介于-1與1之間的值)。
9.7. Math.tan(x)方法可返回一個表示某個角的正切的數字。
9.8. 例子
9.8.1. 代碼
<!DOCTYPE html> <html lang="zh-cn"><head><meta charset="utf-8" /><title>三角函數</title></head><body><script type="text/javascript">document.write('Math.sin(0 * Math.PI / 180) = ' + Math.sin(0 * Math.PI / 180) + '<br />');document.write('Math.sin(30 * Math.PI / 180) = ' + Math.sin(30 * Math.PI / 180) + '<br />'); document.write('Math.sin(45 * Math.PI / 180) = ' + Math.sin(45 * Math.PI / 180) + '<br />'); document.write('Math.sin(60 * Math.PI / 180) = ' + Math.sin(60 * Math.PI / 180) + '<br />');document.write('Math.sin(90 * Math.PI / 180) = ' + Math.sin(90 * Math.PI / 180) + '<br /><hr />');document.write('Math.cos(0 * Math.PI / 180) = ' + Math.cos(0 * Math.PI / 180) + '<br />');document.write('Math.cos(30 * Math.PI / 180) = ' + Math.cos(30 * Math.PI / 180) + '<br />');document.write('Math.cos(45 * Math.PI / 180) = ' + Math.cos(45 * Math.PI / 180) + '<br />');document.write('Math.cos(60 * Math.PI / 180) = ' + Math.cos(60 * Math.PI / 180) + '<br />');document.write('Math.cos(90 * Math.PI / 180) = ' + Math.cos(90 * Math.PI / 180) + '<br /><hr />');document.write('Math.tan(0 * Math.PI / 180) = ' + Math.tan(0 * Math.PI / 180) + '<br />');document.write('Math.tan(30 * Math.PI / 180) = ' + Math.tan(30 * Math.PI / 180) + '<br />');document.write('Math.tan(45 * Math.PI / 180) = ' + Math.tan(45 * Math.PI / 180) + '<br />');document.write('Math.tan(60 * Math.PI / 180) = ' + Math.tan(60 * Math.PI / 180) + '<br />');</script></body> </html>9.8.2. 效果圖
總結
以上是生活随笔為你收集整理的072_Math对象的全部內容,希望文章能夠幫你解決所遇到的問題。