原文

  简书原文:https://www.jianshu.com/p/8776ec9cfb58

大纲

  前言
  1、Math对象的值属性
  2、Math对象的函数属性
  3、Math对象的函数的使用

前言

  Math对象是一个全局的对象,不需要定义一个新的对象可直接使用。
  Math 对象用于执行数学任务。
  Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()。

  1. var resultNum1 = Math.abs(testNum);

1、Math对象的值属性

  1. Math.E : 2.718281828459045
  2. Math.LN10 : 2.302585092994046
  3. Math.LN2 : 0.6931471805599453
  4. Math.LOG2E: 0.6931471805599453
  5. Math.LOG10E: 2.302585092994046
  6. Math.PI: 3.141592653589793
  7. Math.SQRT2 : 1.4142135623730951
  8. Math.SQRT1_2: 0.7071067811865476(1/2的平方根)

2、Math对象的函数属性

  1. abs(x); //绝对值
  2. exp(x); //(e的x次方)
  3. log(x); //(x的自然对数)
  4. max(x1,x2,x3...); //最大值
  5. min(x1,x2,x3...); //最小值
  6. pow(x,y); //(x的y次方)
  7. random(); //(随机数)
  8. sqrt(x); //(x的平方根)
  9.  
  10. //取整
  11. ceil(x);
  12. floor(x);
  13. round(x);
  14.  
  15. //三角函数
  16. cos(x);
  17. sin(x);
  18. tan(x);
  19. acos(x);
  20. asin(x);
  21. atan(x);
  22. atan2(y,2);

3、Math对象的函数的使用

3.1、取整方法:parseInt()、ceil()、floor()、round()

  1. <!--
  2. parseInt():将浮点数转换成整数,直接取整数部分(是JavaScript的内置对象,
  3. 不是Math的方法)
  4. ceil():向上取整
  5. floor():向下取整
  6. round():四舍五入取整
  7. -->
  8. <html>
  9. <head>
  10. <title>Test</title>
  11. </head>
  12. <body>
  13. <script>
  14. var myNumber = prompt("Enter the number to be rounded","");
  15. document.write("<h3>The number you entered was "+myNumber+"</h3><br/>");
  16. document.write("<p>The rounding results for this number are </p>");
  17. document.write("<table width=150 border=1>");
  18. document.write("<tr><th>Method</th><th>Result</th></tr>");
  19. document.write("<tr><td>parseInt()</td><td>"+parseInt(myNumber)+"</td></tr>");
  20. document.write("<tr><td>ceil()</td><td>"+Math.ceil(myNumber)+"</td></tr>");
  21. document.write("<tr><td>floor()</td><td>"+Math.floor(myNumber)+"</td></tr>");
  22. document.write("<tr><td>round()</td><td>"+Math.round(myNumber)+"</td></tr>");
  23. document.write("</table>");
  24. </script>
  25. </body>
  26. </html>

3.2、获取随机数的方法:random()方法

  1. <!--
  2. 随机投掷十次的骰子,获取骰子点数
  3. -->
  4. <html>
  5. <head><title>Test</title></head>
  6. <body>
  7. <script>
  8. var throwCount ;
  9. var diceThrow;
  10. for(throwCount = 0;throwCount<10;throwCount++){
  11. diceThrow = (Math.floor(Math.random() * 6) +1);
  12. document.write(diceThrow +"<br/>");
  13. }
  14. //4 5 4 6 2 1 3 6 4 6

3.3、数的平方方法:pow()方法

  1. <!--
  2. 使用pow()方法模拟fix()方法
  3. -->
  4. <html>
  5. <head><title>Test</title></head>
  6. <body>
  7. <script>
  8. function fix(fixNumber, decimalPlaces){
  9. var div = Math.pow(10,decimalPlaces);
  10. fixNumber = Math.round(fixNumber * div)/div;
  11. return fixNumber;
  12. }
  13.  
  14. var number1 = prompt("Enter the number with decimal places you want to fix","");
  15. var number2 = prompt("How many decimal places do you want?","");
  16.  
  17. document.write(number1 + " fixed to " + number2 + " decimal places is:");
  18. document.write(fix(number1,number2));//234.234234 fixed to 2 decimal places is:234.23
  19. </script>
  20. </body>
  21. </html>

3.4、最大最小方法:min()、max()

  1. /*
  2. 但是由于min()和max()方法只能接受任意多个数值参数,而不能将变量传入,所以一般
  3. 使用apply来重写这个方法
  4. */
  5. var values = [1,5,87,6,45,67];
  6. var max = Math.max(values);
  7. console.log(max);//NaN
  8. var max = Math.max.apply(Math,values);
  9. console.log(max);//87

  

JavaScript的Math对象的更多相关文章

  1. javascript类型系统——Math对象

    × 目录 [1]常量 [2]函数 前面的话 javascript使用算术运算符实现基本的算术运算,如果要实现更加复杂的算术运算,需要通过Math对象定义的常量和函数来实现.和其他对象不同,Math只是 ...

  2. JavaScript数据类型 Math对象详解

    前言 javascript使用算术运算符实现基本的算术运算,如果要实现更加复杂的算术运算,需要通过Math对象定义的常量和函数来实现.和其他对象不同,Math只是一个静态对象,并没有Math()构造函 ...

  3. javascript之Math对象

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  4. JavaScript:Math 对象

    ylbtech-JavaScript:Math 对象 Math 对象用于执行数学任务. 使用 Math 的属性和方法的语法: var pi_value=Math.PI; var sqrt_value= ...

  5. javascript总结3:javaScript的 Math 对象

    Math 对象 Math 对象用于执行数学任务. Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(). Math 常用的方法 var n1=1234; v ...

  6. JavaScript 之 Math对象

    Math对象 Math 对象不是构造函数,它具有数学常数和函数的属性和方法,都是以静态成员的方式提供. 常用方法: Math.PI // 圆周率 Math.random() // 生成随机数,生成0~ ...

  7. JavaScript中Math对象的方法介绍

    1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...

  8. javascript操作Math对象的方法总结

    //数学函数--abs 返回数字的绝对值 var a; /*a = Math.abs(-12); alert(a); //12 //数学函数--acos 返回数的反余弦数 a = Math.acos( ...

  9. JavaScript里Math对象的ceil()、floor()、round()方法的区别

    ceil(x) 官方含义:对一个数进行上舍入.理解:ceiling为天花板的意思,意译为向上取整.即取得大于于等于x的最大整数. floor(x) 官方含义:对一个数进行下舍入.理解:floor为地板 ...

随机推荐

  1. 使用spring-boot 国际化配置所碰到的乱码问题

    写好html静态页面 ,  也加上了编码格式 , 获取国际化展示在浏览器中还是存在乱码 , 开始以为是浏览器编码格式问题 , 做过处理后任没有得到解决 , 具体的处理方案如下: <meta ht ...

  2. 【2017 Multi-University Training Contest - Team 2】 Is Derek lying?

    [Link]: [Description] 两个人都做了完全一样的n道选择题,每道题都只有'A','B','C' 三个选项,,每道题答对的话得1分,答错不得分也不扣分,告诉你两个人全部n道题各自选的是 ...

  3. js17---创建对象:构造函数式和原型组合模式、动态原型模式、稳妥构造函数式

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  4. 2.Java实现基于SOAP的XML文档网络传输及远程过程调用(RPC)-

    转自:https://blog.csdn.net/a214919447/article/details/55260411 SOAP(Simple Object Access Protocol,简单对象 ...

  5. BZOJ 1009 GT考试 (AC自动机 + 矩阵乘法加速dp)

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 准考证号为\(n\)位数\(X_1X_2....X_n(0<=X_ ...

  6. 企业网管软件之SOLARWINDS实战-基于浏览器的网络流量监控

    本文出自 "李晨光原创技术博客" 博客,谢绝转载!

  7. Django路由分配以及模版渲染

    路由上: 在网络上区分不同的电脑通过IP.端口和网卡的MAC地址等,在web框架中怎么区分不同的请求呢,就是通过 ‘url(路由)’ ,url 学名叫做全球统一资源定位符,其实就是一个网址 一个url ...

  8. vim学习4

    分频 参考 http://coolshell.cn/articles/1679.htm

  9. 洛谷—— P1080 国王游戏

    https://www.luogu.org/problem/show?pid=1080 题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整 ...

  10. [Vue + TS] Use Properties in Vue Components Using @Prop Decorator with TypeScript

    With properties we can follow a one-way parent→child flow communication between components. This les ...