写在前面,最最常用几个方法:

绝对值:Math.abs(x)
最大值:Math.max([x[, y[, …]]])
最小值:Math.min([x[, y[, …]]])
随机值:Math.random()
向上取整:Math.ceil(x)
向下取整:Math.floor(x)
四舍五入:Math.round(x)

1.概述

Math是一个内置对象,它拥有一些数学常数属性和数学函数方法。Math 不是一个函数对象。Math 用于 Number 类型。

Math 不是一个构造器。Math 的所有属性与方法都是静态的。Math 的常量是使用 JavaScript 中的全精度浮点数来定义的。

2.方法

需要注意的是,三角函数 sin()cos()tan()asin()acos()atan()atan2()返回的值是弧度而非角度。

若要转换,弧度除以 (Math.PI / 180) 即可转换为角度,同理,角度乘以这个数则能转换为弧度。

很多 Math 函数都有一个精度,而且这个精度在不同实现中也是不相同的。这意味着不同的浏览器会给出不同的结果,甚至,在不同的系统或架构下,相同的 JS 引擎也会给出不同的结果!

2.1 常用方法

Math.abs(x)
  返回一个数的绝对值。

Math.abs('-1');     // 1
Math.abs(-2);       // 2
Math.abs(null);     // 0
Math.abs("string"); // NaN
Math.abs();         // NaN

Math.ceil(x)
  返回大于一个数的最小整数,即一个数向上取整后的值。

Math.ceil(.95);      // expected output: 1
Math.ceil(4);       // expected output: 4
Math.ceil(7.004);   // expected output: 8
Math.ceil(-7.004);  // expected output: -7

Math.floor(x)
  返回小于一个数的最大整数,即一个数向下取整后的值。

Math.floor( 45.95);  // 45
Math.floor( 45.05);     // 45
Math.floor( 4 );        // 4
Math.floor(-45.05);     // -46
Math.floor(-45.95);     // -46

Math.max([x[, y[, …]]])
  返回零到多个数值中最大值。

Math.max(10, 20);   //  20
Math.max(-10, -20); // -10
Math.max(-10, 20);  //  20

Math.min([x[, y[, …]]])
  返回零到多个数值中最小值。

Math.max(10, 20);   //  10
Math.max(-10, -20); // -20
Math.max(-10, 20);  //  -10

Math.random()
  返回一个 0 到 1 ([0, 1))之间的伪随机数。

Math.random();           // eg:0.3807786079066584

Math.round(x)
  返回四舍五入后的整数。

Math.round(20.49);       // 20
Math.round(20.5);       // 21
Math.round(-20.5);      // -20
Math.round(-20.51);     // -21
2.2 其余方法

Math.acos(x)
  返回一个数的反余弦值。

Math.acos(-2);  // NaN
Math.acos(-1);  // 3.141592653589793
Math.acos(0);   // 1.5707963267948966
Math.acos(0.5); // 1.0471975511965979
Math.acos(1);   // 0
Math.acos(2);   // NaN

对于小于 -1 或大于 1 的值,Math.acos 返回 NaN

Math.acosh(x)
  返回一个数的反双曲余弦值。

Math.acosh(0.5); // NaN
Math.acosh(1);   // 0
Math.acosh(2);   // 1.3169578969248166

当参数小于1时, Math.acosh()将返回NaN

Math.asin(x)
  返回一个数的反正弦值。

Math.asin(-2);  // NaN
Math.asin(-1);  // -1.5707963267948966 (-pi/2)
Math.asin(0);   // 0
Math.asin(0.5); // 0.5235987755982989
Math.asin(1);   // 1.570796326794897 (pi/2)
Math.asin(2);   // NaN

对于小于 -1 或大于 1 的参数值,Math.asin 返回 NaN

Math.asinh(x)
  返回一个数的反双曲正弦值。

Math.asinh(1);  // 0.881373587019543
Math.asinh(0);  // 0

Math.atan(x)
  返回一个数的反正切值。

Math.atan(1);  // 0.7853981633974483
Math.atan(0);  // 0

Math.atanh(x)
  返回一个数的反双曲正切值。

Math.atanh(-2);  // NaN
Math.atanh(-1);  // -Infinity
Math.atanh(0);   // 0
Math.atanh(0.5); // 0.5493061443340548
Math.atanh(1);   // Infinity
Math.atanh(2);   // NaN

对于大于1或是小于-1的值,函数返回 NaN

Math.atan2(y, x)
  返回 y/x 的反正切值。

Math.atan2(90, 15) // 1.4056476493802699
Math.atan2(15, 90) // 0.16514867741462683Math.atan2( ±0, -0 )               // ±PI.
Math.atan2( ±0, +0 )               // ±0.
Math.atan2( ±0, -x )               // ±PI for x > 0.
Math.atan2( ±0, x )                // ±0 for x > 0.
Math.atan2( -y, ±0 )               // -PI/2 for y > 0.
Math.atan2( y, ±0 )                // PI/2 for y > 0.
Math.atan2( ±y, -Infinity )        // ±PI for finite y > 0.
Math.atan2( ±y, +Infinity )        // ±0 for finite y > 0.
Math.atan2( ±Infinity, x )         // ±PI/2 for finite x.
Math.atan2( ±Infinity, -Infinity ) // ±3*PI/4.
Math.atan2( ±Infinity, +Infinity ) // ±PI/4.

Math.cbrt(x)
  返回一个数的立方根。cbrt 是 “cube root” 的缩写, 意思是立方根。

Math.cbrt(NaN); // NaN
Math.cbrt(-1); // -1
Math.cbrt(-0); // -0
Math.cbrt(-Infinity); // -Infinity
Math.cbrt(0); // 0
Math.cbrt(1); // 1
Math.cbrt(Infinity); // Infinity
Math.cbrt(null); // 0
Math.cbrt(2);  // 1.2599210498948734

Math.clz32(x)
返回一个 32 位整数的前导零的数量。

Math.clz32(1)                // 31
Math.clz32(1000)             // 22
Math.clz32()                 // 32
[NaN, Infinity, -Infinity, 0, -0, null, undefined, "foo", {}, []].filter(function (n) {return Math.clz32(n) !== 32
})                           // []
Math.clz32(true)             // 31
Math.clz32(3.5)

Math.cos(x)
返回一个数的余弦值。

Math.cos(0);           // 1
Math.cos(1);           // 0.5403023058681398
Math.cos(Math.PI);     // -1
Math.cos(2 * Math.PI); // 1

Math.cosh(x)
返回一个数的双曲余弦值。可通过函数模拟实现:

Math.cosh = Math.cosh || function(x) {var y = Math.exp(x);return (y + 1 / y) / 2;
};
Math.cosh(0);  // 1
Math.cosh(1);  // 1.5430806348152437
Math.cosh(-1); // 1.543080634815243

Math.exp(x)
返回欧拉常数的参数次方,Ex,其中 x 为参数,E 是欧拉常数(2.718…,自然对数的底数)。

Math.exp(-1); // 0.36787944117144233
Math.exp(0);  // 1
Math.exp(1);  // 2.718281828459045

Math.expm1(x)
返回 exp(x) - 1 的值。其中 x 是该函数的参数, E 是自然对数的底数 2.718281828459045.

Math.expm1(1)     // 1.7182818284590453
Math.expm1(-38)   // -1
Math.expm1("-38") // -1
Math.expm1("foo") // NaN

Math.fround(x)
  返回最接近一个数的单精度浮点型表示。

数字1.5可以在二进制数字系统中精确表示,32位和64位的值相同:

Math.fround(1.5); // 1.5
Math.fround(1.5) === 1.5; // true

但是,数字1.337却无法在二进制数字系统中精确表示,所以32位和64位的值是不同的:

Math.fround(1.337); // 1.3370000123977661
Math.fround(1.337) === 1.337; // false

在某些精度不高的场合下,可以通过将二个浮点数转换成32位浮点数进行比较,以解决64位浮点数比较结果不正确的问题:

0.1 + 0.2 == 0.3;    //false
function equal(v1, v2) {return Math.fround(v1) == Math.fround(v2);
}
equal(0.1 + 0.2, 0.3);   //true

Math.hypot([x[, y[, …]]])
  返回其所有参数平方和的平方根。

Math.hypot(3, 4);        // 5
Math.hypot(3, 4, 5);     // 7.0710678118654755
Math.hypot();            // 0
Math.hypot(NaN);         // NaN
Math.hypot(3, 4, 'foo'); // NaN, +'foo' => NaN
Math.hypot(3, 4, '5');   // 7.0710678118654755, +'5' => 5
Math.hypot(-3);          // 3, the same as Math.abs(-3)

Math.imul(x, y)
  返回 32 位整数乘法的结果。

Math.log(x)
  返回一个数的自然对数(㏒e,即 ㏑)。

Math.log(-1); // NaN, out of range
Math.log(0); // -Infinity
Math.log(1); // 0
Math.log(10); // 2.302585092994046

Math.log1p(x)
  返回一个数加 1 的和的自然对数(㏒e,即 ㏑)。

Math.log1p(Math.E-1)  // 1
Math.log1p(0)         // 0
Math.log1p("0")       // 0
Math.log1p(-1)        // -Infinity
Math.log1p(-2)        // NaN
Math.log1p("foo")     // NaN

如果参数的值小于 -1, 则返回 NaN

Math.log10(x)
  返回一个数以 10 为底数的对数。

Math.log10(10)   // 1
Math.log10(100)  // 2
Math.log10("100")// 2
Math.log10(1)    // 0
Math.log10(0)    // -Infinity
Math.log10(-2)   // NaN
Math.log10("foo")// NaN

Math.log2(x)
  返回一个数以 2 为底数的对数。

Math.log2(2)     // 1
Math.log2(1024)  // 10
Math.log2(1)     // 0
Math.log2(0)     // -Infinity
Math.log2(-2)    // NaN
Math.log2("1024")// 10
Math.log2("foo") // NaN

Math.pow(x, y)
  返回一个数的 y 次幂。

Math.pow(7, 3);      // 343
Math.pow(4, 0.5);   // 2
Math.pow(7, -2);    // 0.02040816326530612 (1/49)
Math.pow(-7, 0.5);  // NaN

Math.random()
  返回一个 0 到 1 ([0, 1))之间的伪随机数。

Math.random();           // eg:0.3807786079066584

Math.sign(x)
  返回一个数的符号,得知一个数是正数、负数还是 0。此函数共有5种返回值, 分别是 1, -1, 0, -0, NaN. 代表的各是正数, 负数, 正零, 负零, NaN

Math.sign(3);     //  1
Math.sign(-3);    // -1
Math.sign("-3");  // -1
Math.sign(0);     //  0
Math.sign(-0);    // -0
Math.sign(NaN);   // NaN
Math.sign("foo"); // NaN
Math.sign();      // NaN

Math.sin(x)
  返回一个数的正弦值。

Math.sin(0);           // 0
Math.sin(1);           // 0.8414709848078965
Math.sin(Math.PI / 2); // 1

Math.sinh(x)
  返回一个数的双曲正弦值。
双曲正弦的图像如下:

Math.sinh(0)      // 0
Math.sinh(1)      // 1.1752011936438014
Math.sinh("-1")   // -1.1752011936438014
Math.sinh("foo")  // NaN

Math.sqrt(x)
  返回一个数的平方根。

Math.sqrt(9); // 3
Math.sqrt(2); // 1.414213562373095Math.sqrt(1);  // 1
Math.sqrt(0);  // 0
Math.sqrt(-1); // NaN
Math.sqrt(-0); // -0

Math.tan(x)
返回一个数的正切值。
Math.tanh(x)
  返回一个数的双曲正切值。

Math.tanh(0);        // 0
Math.tanh(Infinity); // 1
Math.tanh(1);        // 0.7615941559557649

Math.toSource()
  返回字符串 “Math”。
Math.trunc(x)
  返回一个数的整数部分,仅仅是删除掉数字的小数部分和小数点,不管参数是正数还是负数。传入该方法的参数会被隐式转换成数字类型。

Math.trunc(13.37)    // 13
Math.trunc(42.84)    // 42
Math.trunc(0.123)    //  0
Math.trunc(-0.123)   // -0
Math.trunc("-1.123") // -1
Math.trunc(NaN)      // NaN
Math.trunc("foo")    // NaN
Math.trunc()         // NaN

3.属性

Math.E
  欧拉常数,也是自然对数的底数,约等于 2.718。
Math.LN2
  2 的自然对数,约等于 0.693。
Math.LN10
  10 的自然对数,约等于 2.303。
Math.LOG2E
  以 2 为底的 E 的对数,约等于 1.443。
Math.LOG10E
  以 10 为底的 E 的对数,约等于 0.434。
Math.PI
  圆周率,一个圆的周长和直径之比,约等于 3.14159。
Math.SQRT1_2
  二分之一 ½ 的平方根,同时也是 2 的平方根的倒数,约等于 0.707。
Math.SQRT2
  2 的平方根,约等于 1.414。

Javascript中Math常用操作,向上取整、向下取整、四舍五入相关推荐

  1. math的向上取整_Javascript中Math常用操作,向上取整、向下取整、四舍五入

    写在前面,最最常用几个方法: 绝对值:Math.abs(x) 最大值:Math.max([x[, y[, -]]]) 最小值:Math.min([x[, y[, -]]]) 随机值:Math.rand ...

  2. JavaScript中的骚操作

    JavaScript中的骚操作--记录自用 JavaScript中的骚操作 数组去重 数组转化为对象(Array to Object) 活用三元表达式 转换为数字类型(Convert to Numbe ...

  3. python取整方式(向上取整/向下取整/四舍五入)

    1.向下取整 向下取整直接用内建的 int() 函数即可: >>> a = 3.75 >>> int(a) 3 2.四舍五入 对数字进行四舍五入用 round() ...

  4. 【python】向上取整 向下取整

    python向上取整 向下取整 向上取整 ceil() 函数返回数字的向上取整整数,就是返回大于等于变量的最近的整数. ceil()是不能直接访问的,需要导入 math 模块. import math ...

  5. JavaScript中的属性操作

    为什么80%的码农都做不了架构师?>>>    JavaScript中的属性操作 一. 原型链 在js中,任何一个对象都有一个prototype属性,在js中记做:_proto_. ...

  6. Java int类型的除法 向上取整 向下取整 保留小数位数

    今天做了一道计算题,用到整数相除的向上取整,突然卡住了.然后查询并试用了如下方法. 一.三目运算法(向上 / 向下取整 快捷.准确) 两个int型 a.b,a / b 向上取整.可以使用 a / b ...

  7. c# 整数除法取整_c# 三种取整方法 向上取整 向下取整 四舍五入

    c# 三种取整方法 向上取整 向下取整 四舍五入 Math.Round:四舍六入五取整 Math.Ceiling:向上取整,只要有小数都加1 Math.Floor:向下取整,总是舍去小数 public ...

  8. html内置时间对象,JavaScript中的常用事件,以及内置对象详解

    原标题:JavaScript中的常用事件,以及内置对象详解 今天是刘小爱自学Java的第81天. 感谢你的观看,谢谢你. 话不多说,开始今天的学习: 学前端有一个非常权威的组织,也就是w3c,其有个专 ...

  9. c# 四舍五入、上取整、下取整

    在处理一些数据时,我们希望能用"四舍五入"法实现,但是C#采用的是"四舍六入五成双"的方法,如下面的例子,就是用"四舍六入五成双"得到的结果 ...

  10. python如何对人数向上取整_python中的向上取整向下取整以及四舍五入的方法

    import math #向上取整 print "math.ceil---" print "math.ceil(2.3) => ", math.ceil( ...

最新文章

  1. 推荐一款 Spring Boot 的 HTTP 客户端框架
  2. 判断单链表中的元素是否递增_检测单链表中是否有环(C语言)
  3. shape的简单用法
  4. MySQL | 数据库的六种约束、表的关系、三大范式
  5. 2019 年度程序员吸金榜:你排第几?
  6. 【Python】一句话 if else 简洁写法
  7. Java直连Access
  8. 解决 U盘安装Windows Server 2012 R2 报错
  9. 做meta分析:使用R软件绘制森林图
  10. 远程桌面连接阿里云服务器
  11. 大疆2018网申之机器学习算法工程师笔试题B卷
  12. CST2018/2020安装注意事项
  13. 你想要的WinForm界面开发教程在这里 - 如何使用自定义用户控件
  14. 【Focal Loss】Focal Loss理解
  15. 箭头跳动动画效果和圆盘动画效果
  16. 操作系统-逻辑地址转换为物理地址Java实现
  17. 单片机万年历c语言程序设计,基于AT89C51单片机的多功能
  18. lbs、agps流程
  19. 解析命运密码--由浅入深教你快速学会推断自己一生运气
  20. NLP之文本分词综述

热门文章

  1. 合并多个文件内容到同一个文件
  2. Lucas-Kanade算法
  3. mysql查询排序(asc,desc)
  4. Apache Ranger:统一授权管理框架
  5. Matisse图片选择
  6. iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 80 -j DNAT ...
  7. 禅道页面无法正常打开
  8. iframe框架自适应大小/全屏显示网页框架的方法
  9. SOT-223 封装尺寸图
  10. R语言学习记录:sample()函数