JS Math

作者: 流着万条永远的河 | 来源:发表于2017-09-12 10:06 被阅读0次

Math对象是JS内置对象,提供一系列数学常数和数学方法。Math对象只提供了静态属性和方法,不用实例化。
有时候涉及到数学运算,就可以用它。

Math
E:
2.718281828459045
LN2
:
0.6931471805599453
LN10
:
2.302585092994046
LOG2E
:
1.4426950408889634
LOG10E
:
0.4342944819032518
PI
:
3.141592653589793
SQRT1_2
:
0.7071067811865476
SQRT2
:
1.4142135623730951

Math.PI
3.141592653589793
Math.PI*3*3                                   //算圆的面积
28.274333882308138

方法

  • round四舍五入
Math.round(4.3)
4
Math.round(-4.3)
-4
Math.round(-4.5)                //不是-5,往大处入。
-4
Math.round(4.6)
5
Math.round(4.5)
5
Math.round(0)
0
  • abs,max,min
    abs求绝对值
Math.abs(-4)
4
Math.max(2,-3,6)
6
Math.min(2,-3,6)
-3
Math.min(-3,-3,-3)
-3
Math.min(-3,-3,'g')
NaN

如何求数组里的最大值和最小值(记住就行):

Math.max.apply(null,[3,-3,2,6,8])
8
Math.min.apply(null,[3,-3,2,6,8])
-3

floor,ceil

floor是返回小于参数值的最大整数:

Math.floor(3.2)
3
Math.floor(-3.2)
-4

ceil返回以大于参数的最小整数

Math.ceil(3.2)
4
Math.ceil(-3.2)
-3
Math.ceil(-3.8)
-3

pow ,sqrt

pow(x,y)就是返回x的y次幂:

Math.pow(2,2)
4
Math.pow(2,3)
8

sqrt返回参数的平方根,负数则返回NaN

Math.sqrt(4)
2
Math.sqrt(-44)
NaN

log,exp

log返回以e为底的自然对数:

Math.log(Math.E)
1
Math.log(10)
2.302585092994046

Math.log(100)/Math.LN10
2                                     //以10为底
Math.log(8)/Math.LN2
3                //以2为底
Math.exp(1)
2.718281828459045                //返回e的参数次方
Math.exp(1)  ===Math.E
true
Math.exp(3)  
20.085536923187668

random

返回0到1之间的一个伪随机数,可能等于0,但一定小于1.

Math.random
function random() { [native code] }
Math.random()
0.2951290495662988
Math.random()
0.2444609277534875
Math.random()
0.5542942123798398
Math.random()*100
47.47271738517216
Math.random()*100
87.89817118274796
Math.floor(Math.random()*100)
25
Math.floor(Math.random()*100)
56
Math.floor(Math.random()*100)
91
Math.floor(Math.random()*5)
3
Math.floor(Math.random()*5)
4
Math.floor(Math.random()*5)
2
Math.floor(Math.random()*5)
2
Math.floor(Math.random()*5)
1
Math.floor(Math.random()*5)
2
Math.floor(Math.random()*5)
2
Math.floor(Math.random()*10)+10
14
Math.floor(Math.random()*10)+10
14

获取一个随机的字符串:

function randomStr(len){
  var str=''
  var dict='abcdefghijklmnopqrstuvwxyz_0123456789'
  
  for(var i=0;i<len;i++){
    var index=Math.floor(Math.random()*dict.length)
    str += dict[index]
  }
  return str
}
var str = randomStr(32)
console.log(str)

相关文章

  • 备忘

    1、js中解决数字计算精度问题,math.js bignumber. math.js https://blog.c...

  • js内置对象

    js的内置对象: Math对象就是js提供的一套关于数学的方法: Math.ceil() 向上取整 Math...

  • js与es6函数用法区别

    js函数 1,Math.abs() 获取绝对值 2,Math.ceil() and Math.floor() 向上...

  • 其他知识点(Math和Date)

    Math Math为js内置的一个对象。其中有很多的方法,比如Math.random() Math.random(...

  • canvas杂笔记-圆形

    js中,Math.PI 计算角度弧度,Math.PI = 3.14 = 180° 绘制圆形: 2*Math.PI ...

  • Js中Math类的常用方法

    js生成随机数主要用了math对象的random方法.用法:Math.random( ) Math.ce...

  • 第十四节: JavaScript日期对象和数学对象

    一. Math对象 一般用来执行js中的数学运算 Math对象的方法 Math.PI 圆周率Math....

  • 第十四节 JavaScript数学对象和日期对象

    一. Math对象 一般用来执行js中的数学运算 Math对象的方法 Math.PI 圆周率 Math...

  • JS之Math

    JS之Math Math本身是一个对象,使用时只需调用其内部的属性和方法即可。 Math中常用的属性: Math....

  • JS MATH

    Math Math对象是JavaScript的内置对象,提供一系列数学常数和数学方法。Math对象只提供了静态的属...

网友评论

      本文标题:JS Math

      本文链接:https://www.haomeiwen.com/subject/hpnejxtx.html