js中的Math

js中的Math

Math.round 取最接近的整数
    Math.round(-2.7) // -3

Math.ceil 向上取整
    Math.ceil(1.1) // 2

Math.floor 向下取整
    Math.floor(1.9) // 1

Math.trunc 取整
    Math.trunc(1111.111) // 1111

Math.sin 接受的参数是弧度
    弧度r和角度g的关系
    r = g*Math.PI/180
    Math.sin(30*Math.PI/180)

Math.cos
    Math.cos(60*Math.PI/180)

Math.sqrt 开平方
    Math.sqrt(4)

Math.cbrt 开三次方
    Math.cbrt(8)

Math.pow 次方
    Math.pow(10,10)         // 10000000000
    10 ** 10                // es7的写法
    Math.pow(16,1 / 4)      // 开四次方

Math.random 随机数
    Math.random()                       // [0,1)之间的随机数
    Math.random() * (20 - 10) + 10      // [10,20)之间的随机数
    Math.random() * (20 - 10 + 1) + 10  // [10,20]之间的随机数

Math.max 取最大值
    Math.max(...[2,1,5,0,2])
    Math.max.apply(Math, [2,1,5,0,2])

Math.min 取最小值
    Math.min(...[2,1,5,0,2])
    Math.min.apply(Math, [2,1,5,0,2])

Math.atan2
    取原点到任意点的弧度
        var vector = {x: Math.sqrt(3), y: 1};
        var dir = Math.atan2(vector.y, vector.x);
        console.log(dir*180/Math.PI); // 30

    取任意两点连成的直线的弧度
        var line = {
            p1: {x: -Math.sqrt(3), y: 0},
            p2: {x: 0, y: 1}
        }
        var dir = Math.atan2(line.p2.y - line.p1.y, line.p2.x- line.p1.x);
        console.log(dir*180/Math.PI); // 30

Math.hypot 取n个值的平方和根
    var p1 = {x: 1, y: 2};
    var p2 = {x: 3, y: 4};
    var dis = Math.hypot(p2.x-p1.x, p2.y-p1.y); // 取两点间的距离

原文地址:https://www.cnblogs.com/ye-hcj/p/10349826.html

时间: 2024-08-27 05:13:46

js中的Math的相关文章

利用js中的Math生成范围随机数

在Math静态类中: random()函数是生成0~1之间(包含0不包含1)的随机数 round(x)函数是对x进行四舍五入 利用这两个函数即可以生成任意范围的随机数 例1:生成5~8之间的随机数 <script type="text/javascript"> var num=Math.random(); var num1=(8-5)*num+5; var num2=Math.round(num1); document.write(num2); </script>

JS中的Math.pow(a,b)方法

定义和用法 pow() 方法可返回 x 的 y 次幂的值. 语法 Math.pow(x,y) 参数 描述 x 必需.底数.必须是数字. y 必需.幂数.必须是数字. 返回值 x 的 y 次幂. 说明 如果结果是虚数或负数,则该方法将返回 NaN.如果由于指数过大而引起浮点溢出,则该方法将返回 Infinity. 实例 在下面的例子中,我们将把 pow() 运用到不同的数字组合上: <script type="text/javascript"> document.write(

JS中String,Math常用函数

String对象: 1.length属性 说明:获取字符串的长度 实例: var str="abc"; var i=str.length;//output:3 2.charAt()方法 说明:从字符串中找出一个指定索引(位置)的字符 实例: var str="abc"; var str1=str.charAt(2);//output:c //字符串索引从0开始 3.indexOf()方法 说明:得到子字符串在母字符串中第一次出现的位置(下标),如找不到则输出&quo

js中的Math对象及属性

提到数学公式什么的相信有一大部分人会一脸懵逼~o(^▽^)o~但是没办法,走上代码这条路就注定了要和数学打交道,学吧同志们,加油o(^▽^)o~ Math对象,提供对数据的数学计算.举个例子: 1 <script type="text/javascript"> 2 var mypi=Math.PI; 3 var myabs=Math.abs(-15); 4 document.write(mypi); 5 document.write(myabs); 6 </scrip

js 中的 Math.ceil() Math.floor Math.round()

alert(Math.ceil(25.9)); //26 alert(Math.ceil(25.5)); //26 alert(Math.ceil(25.1)); //26 alert(Math.round(25.9)); //26 alert(Math.round(25.5)); //26 alert(Math.round(25.1)); //25 alert(Math.floor(25.9)); //25 alert(Math.floor(25.5)); //25 alert(Math.fl

js中的Math对象

绝对值Math.abs() console.log(Math.abs(-25)); console.log(Math.abs('-25'));//存在隐式转换可以求绝对值 console.log(Math.abs('wq'));//结果为NaN  not a number 取整Math.floor()  Math.ceil() console.log(Math.floor(1.9)); 向下取整  floor意为地板 console.log(Math.ceil(1.1)); 向上取整 ceil意

JS中Math对象总结

JS中的Math对象提供对数据的数学计算. Math对象属性 Math对象方法 其中max和min方法都可以传入多个参数并正常运算,其它传入参数过多无意义,只取靠前的参数.

js中的函数,Date对象,Math对象和数组对象

函数就是完成某个功能的一组语句,js中的函数由关键字 function + 函数名 + 一组参数定义;函数在定义后可以被重复调用,通常将常用的功能写成一个函数,利用函数可以使代码的组织结构更多清晰. 其语法结构为 function funName (arg0, arg1, … argN){        //statements    } function say_hello (name, msg){ alert(“hello”+ name + “:”+ msg); } say_hello(“d

js中Math.round、parseInt、Math.floor和Math.ceil小数取整总结(转)

js中Math.round.parseInt.Math.floor和Math.ceil小数取整总结 Math.round.parseInt.Math.floor和Math.ceil 都可以返回一个整数,具体的区别请看下面的总结. 一.Math.round 作用:四舍五入,返回参数+0.5后,向下取整. 如: Math.round(5.57) //返回6 Math.round(2.4) //返回2 Math.round(-1.5) //返回-1 Math.round(-5.8) //返回-6 二.