Math对象重新整理
注意:Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法。这是它与Date,String对象的区别。
Math对象的属性
--Math对象包含的属性大都是数学计算中的可能会用到的一些特殊值。
<script type="text/javascript">
var mypi=Math.PI;
var myabs=Math.abs(-15);
document.write(mypi);
document.write(myabs);
</script>
运行结果:
3.141592653589793
15
Math对象方法
--Math对象,提供对数据的数学计算。
Math 对象方法:
Math.ceil(x)
参数说明:
注意:它返回的是大于或等于x,并且与x最接近的整数。(向上取整)
我们将把 ceil() 方法运用到不同的数字上,代码如下:
<script type="text/javascript">
document.write(Math.ceil(0.8) + "<br />")
document.write(Math.ceil(6.3) + "<br />")
document.write(Math.ceil(5) + "<br />")
document.write(Math.ceil(3.5) + "<br />")
document.write(Math.ceil(-5.1) + "<br />")
document.write(Math.ceil(-5.9))
</script>
运行结果:
1
7
5
4
-5
-5
向下取整floor()
floor() 方法可对一个数进行向下取整。
语法:
1
Math.floor(x)
参数说明:
注意:返回的是小于或等于x,并且与x最接近的整数。
我们将在不同的数字上使用 floor() 方法,代码如下:
<script type="text/javascript">
document.write(Math.floor(0.8)+ "<br>")
document.write(Math.floor(6.3)+ "<br>")
document.write(Math.floor(5)+ "<br>")
document.write(Math.floor(3.5)+ "<br>")
document.write(Math.floor(-5.1)+ "<br>")
document.write(Math.floor(-5.9))
</script>
运行结果:
0
6
5
3
-6
-6
四舍五入round()
round() 方法可把一个数字四舍五入为最接近的整数。
语法:
Math.round(x)
参数说明:
注意:
1. 返回与 x 最接近的整数。
2. 对于 0.5,该方法将进行上舍入。(5.5 将舍入为 6)
3. 如果 x 与两侧整数同等接近,则结果接近 +∞方向的数字值 。(如 -5.5 将舍入为 -5; -5.52 将舍入为 -6),如下图:
把不同的数舍入为最接近的整数,代码如下:
<script type="text/javascript">
document.write(<code>Math.round(1.6)</code>+ "<br>");
document.write(<code>Math.round(2.5)</code>+ "<br>");
document.write(<code>Math.round(0.49)</code>+ "<br>");
document.write(<code>Math.round(-6.4)</code>+ "<br>");
document.write(<code>Math.round(-6.6)</code>);
</script>
运行结果:
2
3
0
-6
-7
随机数 random()
random() 方法可返回介于 0 ~ 1(大于或等于 0 但小于 1 )之间的一个随机数。
语法:
1
Math.random();
注意:返回一个大于或等于 0 但小于 1 的符号为正的数字值。
我们取得介于 0 到 1 之间的一个随机数,代码如下:
<script type="text/javascript">
document.write(Math.random());
</script>
运行结果:
1
0.190305486195328
注意:因为是随机数,所以每次运行结果不一样,但是0 ~ 1的数值。
获得0 ~ 10之间的随机数,代码如下:
<script type="text/javascript">
document.write((Math.random())*10);
</script>
运行结果:
8.72153625893887
Math对象重新整理的更多相关文章
- Javascript进阶篇——( JavaScript内置对象---下)--Math对象---笔记整理
Math对象使用 Math 的属性和方法: <script type="text/javascript"> var mypi=Math.PI; var myabs=Ma ...
- 原生JS:Math对象详解
Math对象 本文参考MDN做的详细整理,方便大家参考MDN Math 也是一个内置对象, 为数学常量和数学函数提供了属性和方法,而不是一个函数对象. 与其它全局对象不同的是, Math 不是一个构造 ...
- JavaScript数组(三)数组对象使用整理
一.数组声明方法1. var a=new Array();2. var a=new Array([size]);3.var a=new Array(['a'],[1],['b'],[123]);4. ...
- #9.6课堂JS总结#变量作用域 date()对象 math()对象
一.变量的作用域 1.JavaScript的作用域链 首先看下下面这段代码: <script type="text/javascript"> var rain = 1; ...
- JS学习笔记10之Math对象
-->Math对象 常用属性和方法-->使用Math对象制作相应的效果 Math对象用于执行数学任务 一.Math对象的属性: 二.Math对象的方法: 三.常用属性和方法: Math.P ...
- js 中 Math对象
Math 对象是一个固有的对象,无需创建它,直接把 Math 作为对象使用就可以调用其所有属性和方法.这是它与Date,String对象的区别. Math 对象属性 Math 对象方法
- js入门篇之Math对象
Math对象用于执行数学任务 Math对象的属性: Math对象的方法: 常用属性和方法: Math.PI ----------------返回圆周率3.14 ... Math.ceil(x) --- ...
- 如何使用Math对象快速计算数组中的最大值或最小值
Math 对象下包含 min() 和 max() 方法 用于确定一组数值中的最大值和最小值.这两个方法都可以接收任意多个数值参数. var max = Math.max(1,2,3,4,5,6); c ...
- javascript类型系统——Math对象
× 目录 [1]常量 [2]函数 前面的话 javascript使用算术运算符实现基本的算术运算,如果要实现更加复杂的算术运算,需要通过Math对象定义的常量和函数来实现.和其他对象不同,Math只是 ...
随机推荐
- axios采坑之路
POST请求设置Content-Type 由于后端采用的是form表单形式上送参数,需要设置Content-Type axios设置如下 const _axios = axios.create(con ...
- HDU 1800 Flying to the Mars Trie或者hash
http://acm.hdu.edu.cn/showproblem.php?pid=1800 题目大意: 又是废话连篇 给你一些由数字组成的字符串,判断去掉前导0后那个字符串出现频率最高. 一开始敲h ...
- 剪枝法观点下的旅行商问题(TSP)
1. 构建基本的穷举搜索骨架 int n; int dst[100][100]; int best; const int INF = 987654321; // 初始状态下,path 存入第一节点,v ...
- html表单元素及表单元素详解
原文 https://www.jianshu.com/p/b427daa8663d 大纲 1.认识表单 2.认识表单元素 3.表单元素的分类 4.表单元素——文本框 5.表单元素button 6.表单 ...
- Uploadify404无效链接
Uploadify404无效链接 在使用Jquery Uploadify插件的時候.会发如今请求中有个返回值为404的请求. 假如如今的location为www.aa.com/bugs/more. h ...
- [RxJS] Convert RxJS Subjects to Observables
The use of RxJS Subjects is common, but not without problems. In this lesson we will see how they ca ...
- [Angular] Zones and NgZone
NgZone, Angular uses it to profiling all the async actions such as setTimeout, http request and anim ...
- [AngularFire2] Building an Authentication Observable Data Service
After successfully login, we want something help to check whether user has already login or not. And ...
- ios开发事件处理之 :二:事件的产生与传递
1.事件是怎么样产生与传递的? 当发生一个触摸事件后,系统会将该事件加入到一个由UIApplication管理的事件队列中.(队列是先进先出,而栈是先进后出) UIApplication会从事件队列中 ...
- [Grid Layout] Use auto-fill and auto-fit if the number of repeated grid tracks is not to be def
What about the situation in which we aren’t specifying the number of columns or rows to be repeated? ...