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. Notification通知创建

    Notification通知创建 由于通知是一个远程视图,所以创建通知在状态栏显示需要用到三个主要的对象: 一.PendingIntent对象,用来承载Intent对象的,Intent对象主要是定义通 ...

  2. AS3实现ToolTip效果

    AS3核心类中没有ToolTip类,Flex中的ToolTip类没法用在AS3工程中,Aswing的JToolTip不错,不过如果仅仅为了使用这一个类而导入Aswing就不太明智了.由于最近的项目需要 ...

  3. phpstorm的破解

    按照PHPstorm进入如下页面: 然后继续单击License  server  输入:http://www.0-php.com:1017    PHPstorm完美运行!!!!!

  4. 352. Data Stream as Disjoint Interval

    Given a data stream input of non-negative integers a1, a2, ..., an, ..., summarize the numbers seen ...

  5. Linux硬件监控

    https://blog.csdn.net/qq_30353203/article/details/62222882

  6. Meteor ToDo App实例

    在本章中,我们将创建一个简单的待办事项应用程序. 第1步 - 创建应用程序 打开命令提示符,运行以下命令 - C:\Users\Administrator\Desktop>meteor crea ...

  7. 【Nginx】如何使用http配置

    处理http配置项可以分为下面4个步骤: 1)创建数据结构用于存储配置项对应的参数 2)设定配置项在nginx.conf中出现时的限制条件与回调方法 3)实现第2步中的回调方法,或者使用Nginx框架 ...

  8. 【PostgreSQL】PostgreSQL操作-psql基本命令

    在阅读的过程中有不论什么问题,欢迎一起交流 邮箱:1494713801@qq.com    QQ:1494713801 一.建立数据库连接 ---------------- 接入PostgreSQL数 ...

  9. CodeForces 300C Beautiful Numbers(乘法逆元/费马小定理+组合数公式+高速幂)

    C. Beautiful Numbers time limit per test 2 seconds memory limit per test 256 megabytes input standar ...

  10. HDU 4897 Little Devil I 树链剖分+线段树

    Little Devil I Problem Description There is an old country and the king fell in love with a devil. T ...