欢迎光临
我们一直在努力

JavaScript中Math对象怎么用

这篇文章主要介绍了JavaScript中Math对象怎么用,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。

Math对象

属性:

Math对象方法:

三角函数(sin(), cos(), tan(),asin(), acos(), atan(), atan2())是以弧度返回值的。可以通过除法Math.PI / 180把弧度转换为角度,也可以通过其他方法来转换。

方法 说明
Math.abs(x) 返回x的绝对值.
Math.acos(x) 返回x的反余弦值.
Math.acosh(x) 返回x的反双曲余弦值.
Math.asin(x) 返回x的反正弦值.
Math.asinh(x) 返回x的反双曲正弦值.
Math.atan(x) 以介于 -PI/2 与 PI/2 弧度之间的数值来返回 x 的反正切值.
Math.atanh(x) 返回 x 的反双曲正切值.
Math.atan2(x, y) 返回 y/x 的反正切值.
Math.cbrt(x) 返回x的立方根.
Math.ceil(x) 返回x向上取整后的值.
Math.clz32(x) Returns the number of leading zeroes of a 32-bit integer.
Math.cos(x) 返回x的余弦值.
Math.cosh(x) 返回x的双曲余弦值.
Math.exp(x) 返回 Ex, 当x为参数, E 是欧拉常数 (2.718…), 自然对数的底.
Math.expm1(x) 返回 exp(x)-1 的值.
Math.floor(x) 返回小于x的最大整数。
Math.fround(x) Returns the nearest single precision float representation of a number.
Math.hypot([x[,y[,…]]]) Returns the square root of the sum of squares of its arguments.
Math.imul(x) Returns the result of a 32-bit integer multiplication.
Math.log(x) Returns the natural logarithm (loge, also ln) of a number.
Math.log1p(x) Returns the natural logarithm of 1 + x (loge, also ln) of a number.
Math.log10(x) Returns the base 10 logarithm of x.
Math.log2(x) Returns the base 2 logarithm of x.
Math.max([x[,y[,…]]]) 返回0个到多个数值中最大值.
Math.min([x[,y[,…]]]) 返回0个到多个数值中最小值.
Math.pow(x,y) 返回x的y次幂.
Math.random() 返回0到1之间的伪随机数. 可能等于0,但是一定小于1
Math.round(x) 返回四舍五入后的整数.但是Math.round(-4.40)值为-4
Math.sign(x) 返回x的符号函数, 判定x是正数,负数还是0.
Math.sin(x) 返回正弦值.
Math.sinh(x) 返回x的双曲正弦值.
Math.sqrt(x) 返回x的平方根.
Math.tan(x) 返回x的正切值.
Math.tanh(x) 返回x的双曲正切值.
Math.toSource() 返回字符串 “Math”.
Math.trunc(x) 返回x的整数部分,去除小数.

例子1:写一个函数,返回从min到max之间的随机整数,包括min不包括max

function getRandomArbitrary(min, max) {
  return min + Math.random() * (max - min);}

例子2:写一个函数,生成一个长度为 n 的随机字符串,字符串字符的取值范围包括0到9,a到 z,A到Z

function getRandomInt(min, max) {
  return min + Math.floor(Math.random() * (max - min + 1));}function randomStr(n){
  var dict = '1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  var str = '';
  for(i = 0; i < n;i++){
    str += dict[getRandomInt(0,62)];
  }
  return str;}var str = getRandStr(10);console.log(str);

感谢你能够认真阅读完这篇文章,希望小编分享的“JavaScript中Math对象怎么用”这篇文章对大家有帮助,同时也希望大家多多支持云搜网,关注云搜网行业资讯频道,更多相关知识等着你来学习!

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。