JavaScript中Math对象怎么用

发布时间:2022-03-25 10:05:51 作者:小新
来源:亿速云 阅读:100

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

Math对象

属性:
JavaScript中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对象怎么用”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!

推荐阅读:
  1. Math数学对象
  2. 使用JavaScript中Math对象的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

javascript math

上一篇:JavaScript中String对象是什么

下一篇:JavaScript中数组对象的示例分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》