Math 对象

Math 对象用于执行数学任务。

1.常用属性:

  1.E :返回算术常量e,即自然对数的底数(约2.718)

  2.PI :返回圆周率,约3.14159

2.常用方法    Math.方法()  调用即可

  1.abs(x)  返回绝对值

  2.ceil(x)   上舍入

  3.floor(x)  下舍入

  4.round(x)  四舍五入为最近的整数

  5.random()  返回0~1之间的随机数

  6.max(x,y)  返回x,y中最高值

  7.min(x,y)   返回x,y中最低值

  8.pow(x,y)  返回x的y次幂

  9.sqrt(x)    返回x的平方根

  

 向下取整(舍掉小数)

  Math.floor(2)=2
  Math.floor(2.9)=2
  Math.floor(-2.1)=-3
  Math.floor(-2.9)=-3

 向上取整(凑整)

  Math.ceil(2)=2
  Math.ceil(2.1)=3
  Math.ceil(2.5)=3
  Math.ceil(2.9)=3

  Math.ceil(-2)=-2
  Math.ceil(-2.1)=-2
  Math.ceil(-2.5)=-2
  Math.ceil(-2.9)=-2

  四舍五入(取最近整数)

  Math.round(3.14)=3
  Math.round(3.5)=4
  Math.round(-3.14)=-3
  Math.round(-3.51)=-4

 注意:对于 0.5,该方法将进行上舍入

 例如,3.5 将舍入为 4,而 -3.5 将舍入为 -3。

  Math.random()返回指定min~max之间的随机数

  var x = Math.floor(Math.random()*(max - min + 1)) + min;

从数组中的随机获取成员

  var items = [some 成员];

  var randomItem = items[Math.floor(Math.random() * items.length)];

Math对象常用方法(取整细节)的更多相关文章

  1. JS取整,四舍五入,取绝对值等Math对象常用方法

    function f1(type,num1) { switch(type) { case 'floor': return Math.floor(num1);//取整或下舍入 break; case ' ...

  2. js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)

    js中Math.round.parseInt.Math.floor和Math.ceil小数取整总结 Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数 ...

  3. JS对象 向上取整ceil() ceil() 方法可对一个数进行向上取整。 语法: Math.ceil(x) 注意:它返回的是大于或等于x,并且与x最接近的整数。

    向上取整ceil() ceil() 方法可对一个数进行向上取整. 语法: Math.ceil(x) 参数说明: 注意:它返回的是大于或等于x,并且与x最接近的整数. 我们将把 ceil() 方法运用到 ...

  4. js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结

    Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整 ...

  5. Math对象常用方法汇总

    前几天翻阅<JavaScript权威指南>,看到了Math对象,于是汇总了一下. Math对象不同于其他的对象,它可以说是一个公共数学类,里面有很多数学方法,用于各种数学运算,但是Math ...

  6. js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结

    以前经常在代码中看到Math.round.parseInt.Math.floor和Math.ceil这四个函数,虽然知道结果都可以返回一个整数,但是对他们四者的区别还是不太清楚,今天就做一个小结. 一 ...

  7. js中Math.round、parseInt、Math.floor和Math.ceil小数取整小结【转】

    [摘要:之前常常正在代码中看到Math.round.parseInt.Math.floor战Math.ceil那四个函数,固然晓得效果皆能够返回一个整数,然则对他们四者的差别照样没有太清晰,本日便做一 ...

  8. Math对象常用方法

    1.Math.ceil(x) 返回x的向上取整. var a=Math.ceil(9.1); var b=Math.ceil(-9.1) console.log(a); console.log(b); ...

  9. JavaScript Math 对象常用方法

    Math.abs(x):可返回数的绝对值 Math.ceil(x):向上取整 Math.floor(x):向下取整 Math.max(x,y):最大值 Math.min(x,y):最小值 Math.r ...

随机推荐

  1. Codeforces 618C(计算几何)

    C. Constellation time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  2. Partition List(链表的插入和删除操作,找前驱节点)

    Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...

  3. POJ 1062 【带约束的最短路问题】

    中文题题意不写. 建图: 我自己想到的建图方式是把每个物品看作两个点,编号分别是i和i+n,然后每个物品两个点之间边的权值是物品本身的价值.然后从第i个点往外连边,目标是可替代品顶点编号较小的点,权值 ...

  4. C# DataGridView,右键单击RowHeader时显示右键菜单怎么做?

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)         {       ...

  5. 【APUE】进程间通信之共享存储(mmap函数)

    共享内存可以说是最有用的进程间通信方式,也是最快的IPC形式,因为进程可以直接读写内存,而不需要任何数据的拷贝.对于像管道和消息队列等通信方式,则需要在内核和用户空间进行四次的数据拷贝,而共享内存则只 ...

  6. POJ-2240 -Arbitrage(Bellman)

    题目链接:Arbitrage 让这题坑了,精度损失的厉害.用赋值的话.直接所有变成0.00了,无奈下,我仅仅好往里输了,和POJ1860一样找正环,代码也差点儿相同,略微改改就能够了,可是这个题精度损 ...

  7. Leetcode--easy系列10

    #205 Isomorphic Strings Given two strings s and t, determine if they are isomorphic. Two strings are ...

  8. Atitit.软件仪表盘(7)--温度监測子系统--电脑重要部件温度与监控and警报

    Atitit.软件仪表盘(7)--温度监測子系统--电脑重要部件温度与监控and警报 Cpu温度.风扇转速 主板温度 显卡温度 硬盘温度 电池温度 鲁大师  硬盘温度 Cpu温度  core temp ...

  9. Find Minimum in Rotated Sorted Array 旋转数组中找最小值 @LeetCode

    O(n)的算法就不说了,这题主要考查的是 O(logn)的算法. 有序数组easy想到使用二分查找解决.这题就是在二分基础上做一些调整.数组仅仅有一次翻转,能够知道原有序递增数组被分成两部分,这俩部分 ...

  10. 使用mongostat监视mongodb

    1, 监视一个mongod mongostat 10.80.1.1:27018 1,监视replica set mongostat --host rs0/10.80.1.1:27018,10.80.1 ...