JavaScript的Math对象
原文
简书原文:https://www.jianshu.com/p/8776ec9cfb58
大纲
前言
1、Math对象的值属性
2、Math对象的函数属性
3、Math对象的函数的使用
前言
Math对象是一个全局的对象,不需要定义一个新的对象可直接使用。
Math 对象用于执行数学任务。
Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math()。
- var resultNum1 = Math.abs(testNum);
1、Math对象的值属性
- Math.E : 2.718281828459045
- Math.LN10 : 2.302585092994046
- Math.LN2 : 0.6931471805599453
- Math.LOG2E: 0.6931471805599453
- Math.LOG10E: 2.302585092994046
- Math.PI: 3.141592653589793
- Math.SQRT2 : 1.4142135623730951
- Math.SQRT1_2: 0.7071067811865476(1/2的平方根)
2、Math对象的函数属性
- abs(x); //绝对值
- exp(x); //(e的x次方)
- log(x); //(x的自然对数)
- max(x1,x2,x3...); //最大值
- min(x1,x2,x3...); //最小值
- pow(x,y); //(x的y次方)
- random(); //(随机数)
- sqrt(x); //(x的平方根)
- //取整
- ceil(x);
- floor(x);
- round(x);
- //三角函数
- cos(x);
- sin(x);
- tan(x);
- acos(x);
- asin(x);
- atan(x);
- atan2(y,2);
3、Math对象的函数的使用
3.1、取整方法:parseInt()、ceil()、floor()、round()
- <!--
- parseInt():将浮点数转换成整数,直接取整数部分(是JavaScript的内置对象,
- 不是Math的方法)
- ceil():向上取整
- floor():向下取整
- round():四舍五入取整
- -->
- <html>
- <head>
- <title>Test</title>
- </head>
- <body>
- <script>
- var myNumber = prompt("Enter the number to be rounded","");
- document.write("<h3>The number you entered was "+myNumber+"</h3><br/>");
- document.write("<p>The rounding results for this number are </p>");
- document.write("<table width=150 border=1>");
- document.write("<tr><th>Method</th><th>Result</th></tr>");
- document.write("<tr><td>parseInt()</td><td>"+parseInt(myNumber)+"</td></tr>");
- document.write("<tr><td>ceil()</td><td>"+Math.ceil(myNumber)+"</td></tr>");
- document.write("<tr><td>floor()</td><td>"+Math.floor(myNumber)+"</td></tr>");
- document.write("<tr><td>round()</td><td>"+Math.round(myNumber)+"</td></tr>");
- document.write("</table>");
- </script>
- </body>
- </html>
3.2、获取随机数的方法:random()方法
- <!--
- 随机投掷十次的骰子,获取骰子点数
- -->
- <html>
- <head><title>Test</title></head>
- <body>
- <script>
- var throwCount ;
- var diceThrow;
- for(throwCount = 0;throwCount<10;throwCount++){
- diceThrow = (Math.floor(Math.random() * 6) +1);
- document.write(diceThrow +"<br/>");
- }
- //4 5 4 6 2 1 3 6 4 6
3.3、数的平方方法:pow()方法
- <!--
- 使用pow()方法模拟fix()方法
- -->
- <html>
- <head><title>Test</title></head>
- <body>
- <script>
- function fix(fixNumber, decimalPlaces){
- var div = Math.pow(10,decimalPlaces);
- fixNumber = Math.round(fixNumber * div)/div;
- return fixNumber;
- }
- var number1 = prompt("Enter the number with decimal places you want to fix","");
- var number2 = prompt("How many decimal places do you want?","");
- document.write(number1 + " fixed to " + number2 + " decimal places is:");
- document.write(fix(number1,number2));//234.234234 fixed to 2 decimal places is:234.23
- </script>
- </body>
- </html>
3.4、最大最小方法:min()、max()
- /*
- 但是由于min()和max()方法只能接受任意多个数值参数,而不能将变量传入,所以一般
- 使用apply来重写这个方法
- */
- var values = [1,5,87,6,45,67];
- var max = Math.max(values);
- console.log(max);//NaN
- var max = Math.max.apply(Math,values);
- console.log(max);//87
JavaScript的Math对象的更多相关文章
- javascript类型系统——Math对象
× 目录 [1]常量 [2]函数 前面的话 javascript使用算术运算符实现基本的算术运算,如果要实现更加复杂的算术运算,需要通过Math对象定义的常量和函数来实现.和其他对象不同,Math只是 ...
- JavaScript数据类型 Math对象详解
前言 javascript使用算术运算符实现基本的算术运算,如果要实现更加复杂的算术运算,需要通过Math对象定义的常量和函数来实现.和其他对象不同,Math只是一个静态对象,并没有Math()构造函 ...
- javascript之Math对象
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JavaScript:Math 对象
ylbtech-JavaScript:Math 对象 Math 对象用于执行数学任务. 使用 Math 的属性和方法的语法: var pi_value=Math.PI; var sqrt_value= ...
- javascript总结3:javaScript的 Math 对象
Math 对象 Math 对象用于执行数学任务. Math 对象并不像 Date 和 String 那样是对象的类,因此没有构造函数 Math(). Math 常用的方法 var n1=1234; v ...
- JavaScript 之 Math对象
Math对象 Math 对象不是构造函数,它具有数学常数和函数的属性和方法,都是以静态成员的方式提供. 常用方法: Math.PI // 圆周率 Math.random() // 生成随机数,生成0~ ...
- JavaScript中Math对象的方法介绍
1.比较最值方法 比较最值有两种方法,max() 和 min() 方法. 1.1 max() 方法,比较一组数值中的最大值,返回最大值. var maxnum = Math.max(12,6,43,5 ...
- javascript操作Math对象的方法总结
//数学函数--abs 返回数字的绝对值 var a; /*a = Math.abs(-12); alert(a); //12 //数学函数--acos 返回数的反余弦数 a = Math.acos( ...
- JavaScript里Math对象的ceil()、floor()、round()方法的区别
ceil(x) 官方含义:对一个数进行上舍入.理解:ceiling为天花板的意思,意译为向上取整.即取得大于于等于x的最大整数. floor(x) 官方含义:对一个数进行下舍入.理解:floor为地板 ...
随机推荐
- 使用spring-boot 国际化配置所碰到的乱码问题
写好html静态页面 , 也加上了编码格式 , 获取国际化展示在浏览器中还是存在乱码 , 开始以为是浏览器编码格式问题 , 做过处理后任没有得到解决 , 具体的处理方案如下: <meta ht ...
- 【2017 Multi-University Training Contest - Team 2】 Is Derek lying?
[Link]: [Description] 两个人都做了完全一样的n道选择题,每道题都只有'A','B','C' 三个选项,,每道题答对的话得1分,答错不得分也不扣分,告诉你两个人全部n道题各自选的是 ...
- js17---创建对象:构造函数式和原型组合模式、动态原型模式、稳妥构造函数式
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...
- 2.Java实现基于SOAP的XML文档网络传输及远程过程调用(RPC)-
转自:https://blog.csdn.net/a214919447/article/details/55260411 SOAP(Simple Object Access Protocol,简单对象 ...
- BZOJ 1009 GT考试 (AC自动机 + 矩阵乘法加速dp)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1009 题意: 准考证号为\(n\)位数\(X_1X_2....X_n(0<=X_ ...
- 企业网管软件之SOLARWINDS实战-基于浏览器的网络流量监控
本文出自 "李晨光原创技术博客" 博客,谢绝转载!
- Django路由分配以及模版渲染
路由上: 在网络上区分不同的电脑通过IP.端口和网卡的MAC地址等,在web框架中怎么区分不同的请求呢,就是通过 ‘url(路由)’ ,url 学名叫做全球统一资源定位符,其实就是一个网址 一个url ...
- vim学习4
分频 参考 http://coolshell.cn/articles/1679.htm
- 洛谷—— P1080 国王游戏
https://www.luogu.org/problem/show?pid=1080 题目描述 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整 ...
- [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 ...