javascript中Number对象的方法和Math对象的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。联系[email protected] https://blog.csdn.net/div_ma/article/details/74063061
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
    </body>
    <script>
//  Number对象 
    var num =new Number(1234);
    //toString 把数字转换为字符串;使用指定的基数.
    console.log(typeof num.toString());
    //  输出的结果是string

    //toLocaleString 把数字转换为字符串 使用本地数字格式顺序
    console.log(typeof num.toLocaleString());
    //输出的结果也是string

    //toFixed 把数字转换为字符串,结果的小数点后有指定的位数的数字.
    console.log(num.toFixed(3));
//  输出的结果是 1234.000


    //toExponential 把对象的值转换为指数计数法
    console.log(num.toExponential(4));
    //输出的结果是1.2340e+3


    //toPrecision 把数字格式化为指定的长度
    console.log(num.toPrecision(9));
    //输出的结果是1234.00000


    //valueOf 返回一个Number对象的基本数字值.
    console.log(num.valueOf());
    //输出的结果是number格式的 1234;


    /*
     * 常用的Math方法;
     */
    //1.ceil 对数进行向上取整 
        console.log(Math.ceil(1.1));
    //      输出的结果是2 无论小数点后是多少 都是向上取整数

    //2.floor 对数字进行向下取整
        console.log(Math.floor(1.9));
    //      输出的结果是1  无论小数点后面的数字是多少 都是向下取整数 

    //3.round 对数字进行四舍五入
    console.log(Math.round(1.5));
    console.log(Math.round(1.4));
    //  输出的结果是2 和1 对数字进行四舍五入;

    // 4.random() 返回0~1之间的随机数;

    console.log(Math.random());
    //  输入的结果是0到1之间的浮点数 不包括0和1

    //max(x,y) 可用返回x和y中的最高值
    console.log(Math.max(7,14,8,99,65))
    //  输出的结果是99

    console.log(Math.min(8,15,99,752,185,8))
    //输出的结果是8


    </script>
</html>

猜你喜欢

转载自blog.csdn.net/div_ma/article/details/74063061
今日推荐