• JavaScript 数字方法


    toString()

    将数字转换为字符串。

    1. var myNumber = 32;
    2. console.log(myNumber.toString()); // 32

    进制转换

    1. var myNumber = 32;
    2. console.log("(十进制)Decimal " + myNumber.toString(10)); // Decimal 32
    3. console.log("(十六进制)Hexadecimal " + myNumber.toString(16)); // Hexadecimal 20
    4. console.log("(八进制)Octal " + myNumber.toString(8)); // Octal 40
    5. console.log("(二进制)Binary " + myNumber.toString(2)); // Binary 100000

    toExponential() 

    返回一个字符串,包含进行舍入的数字并使用指数表示法,可选参数定义小数点后面的位数。

    1. var x = 9.656;
    2. console.log(x.toExponential()); // 9.656e+0
    3. console.log(x.toExponential(2)); // 9.66e+0
    4. console.log(x.toExponential(4)); // 9.6560e+0
    5. console.log(x.toExponential(6)); // 9.656000e+0

    toFixed()

    将数字四舍五入为给定的位数(几位小数)。

    1. var x = 9.656;
    2. console.log(x.toFixed(0)); // 10
    3. console.log(x.toFixed(2)); // 9.66
    4. console.log(x.toFixed(4)); // 9.6560
    5. console.log(x.toFixed(6)); // 9.656000

    toPrecision()

    返回一个指定数字数量的字符串,数字指定为多少,小数点前后数字共有多少个。

    1. var x = 9.656;
    2. console.log(x.toPrecision()); //9.656
    3. console.log(x.toPrecision(2)); //9.7
    4. console.log(x.toPrecision(5)); //9.6560
    5. x = 99.656;
    6. console.log(x.toPrecision(5)); //99.656

    Number()

    把变量转换为数字。

    1. console.log(Number(true)); // 1
    2. console.log(Number(true)); // 0
    3. console.log(Number("10.3")); // 10.3
    4. console.log(Number("10,3")); // NaN
    5. console.log(Number("Bill")); // NaN

    parseInt()

    把字符串转换为数字。数字开头的字符串都可以转换为数字。

    1. console.log(parseInt("10")); //10
    2. console.log(parseInt("10.11")); //10
    3. console.log(parseInt("10 years")); //10
    4. console.log(parseInt("10years")); //10
    5. console.log(parseInt("10 att - 你好")); //10
    6. console.log(parseInt("years 10")); //NaN

    parseFloat()

    字符串转换为浮点数字。

    1. console.log(parseFloat("10")); //10
    2. console.log(parseFloat("10.11")); //10.11
    3. console.log(parseFloat("10 years")); //10
    4. console.log(parseFloat("10years")); //10
    5. console.log(parseFloat("10 att - 你好")); //10
    6. console.log(parseFloat("years 10")); //NaN

    Number.MAX_VALUE

    返回 Javascript 中可能的最大值。

    1. var x = Number.MAX_VALUE;
    2. console.log(x); // 1.7976931348623157e+308

    Number.MIN_VALUE

    返回 JavaScript 中可能的最小数字。

    1. var x = Number.MIN_VALUE;
    2. console.log(x); // 5e-324

    Number.POSITIVE_INFINITY

    表示无穷大

    1. var x = Number.POSITIVE_INFINITY;
    2. console.log(x); // Infinity

    溢出时返回 POSITIVE_INFINITY:

    1. var x = 1 / 0;
    2. console.log(x); // Infinity

    Number.NEGATIVE_INFINITY

    表示负无穷大。

    1. var x = Number.NEGATIVE_INFINITY;
    2. console.log(x); // -Infinity

     溢出时返回 NEGATIVE_INFINITY:

    1. var x = -1 / 0;
    2. console.log(x); // -Infinity

    MAX_VALUE

    对变量、表达式或值使用数字属性,将返回 undefined:

    1. var x = 6;
    2. console.log(x.MAX_VALUE); // undefined

  • 相关阅读:
    【Linux】CentOS-6.8超详细安装教程
    剑指 Offer 10- I. 斐波那契数列
    虚拟机安装zookeeper集群
    1、JDK安装及Java开发环境变量配置
    简述Redis事务实现
    OpenMLDB 线上引擎资源需求预估模型,助你快速预估资源消耗
    860. 柠檬水找零
    Mybatis的类型转换注册类TypeHandlerRegistry
    月度总结 | 2022年08月 | 第一段实习经历总结 | 技术栈学习总结
    2022年危险化学品经营单位安全管理人员考题及答案
  • 原文地址:https://blog.csdn.net/qq_40116418/article/details/127683748