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. Spring注解 @Component、@Repository、@Service、@Controller @Resource、@Autowired、@Qualifier 解析

    @Repository.@Service.@Controller 这几个是一个类型,其实@Component 跟他们也是一个类型的 Spring 2.5 中除了提供 @Component 注释外,还定 ...

  2. php.ini中extension默许的地址到底在哪里设置的

    原文: http://www.myexception.cn/php/1436096.html ----------------------------------------------------- ...

  3. ElasticSearch(5.5.2)在java中的使用

    ElasticSearch(5.5.2)在java中的使用 https://blog.csdn.net/didiaodeabing/article/details/79310710 pom.xml: ...

  4. [Angular] Expose Angular Component Logic Using State Reducers

    A component author has no way of knowing which state changes a consumer will want to override, but s ...

  5. Prime Distance(二次筛素数)

    Description The branch of mathematics called number theory is about properties of numbers. One of th ...

  6. python菜鸟日记1

    1. 在__init__(self,...)初始化函数中.定义对象的属性,这些属性不用所有写在括号里,括号里的表示定义的时候须要赋初始值的一些属性,而其它不用初始值的属性.能够 直接卸载代码块中,se ...

  7. NOI 2015 滞后赛解题报告

    报同步赛的时候出了些意外.于是仅仅能做一做"滞后赛"了2333 DAY1 T1离线+离散化搞,对于相等的部分直接并查集,不等部分查看是否在同一并查集中就可以,code: #incl ...

  8. 关于MySQL的TPS和QPS

    TPS - Transactions Per Second(每秒传输的事物处理个数),这是指server每秒处理的事务数,支持事务的存储引擎如InnoDB等特有的一个性能指标. 计算方法: TPS = ...

  9. PyTorch 60 分钟入门教程:数据并行处理

    可选择:数据并行处理(文末有完整代码下载) 作者:Sung Kim 和 Jenny Kang 在这个教程中,我们将学习如何用 DataParallel 来使用多 GPU. 通过 PyTorch 使用多 ...

  10. HTML5你必须知道的28个新特性

    1. 新的Doctype 尽管使用<!DOCTYPE html>,即使浏览器不懂这句话也会按照标准模式去渲染 2. Figure元素 用<figure>和<figcapt ...