Number 对象是原始数值的包装对象
var num = new Number(value);
Number.MAX_VALUE | 可表示的最大的数,MAX_VALUE 属性值接近于 1.79E+308。 大于 MAX_VALUE 的值代表 "Infinity"。 |
Number.MIN_VALUE | 可表示的最小的数,即最接近 0 的正数 (实际上不会变成 0)。最大的负数是 -MIN_VALUE,MIN_VALUE 的值约为 5e-324。小于 MIN_VALUE ("underflow values") 的值将会转换为 0。 |
Number.NEGATIVE_INFINITY | 负无穷大,溢出时返回该值。该值小于 MIN_VALUE |
Number.POSITIVE_INFINITY | 正无穷大,溢出时返回该值。该值大于 MAX_VALUE。 |
Number.NaN | 非数字值(Not-A-Number) |
把对象的值转换为指数计数法。
- var num1 = 1225.30
- var val = num1.toExponential();
- console.log(val)
- // 1.2253e+3
把数字转换为字符串,并对小数点指定位数
- var num3 = 177.234;
- console.log(]num3.toFixed(6))
- //177.234000
指定有效数字
- var num1 = 1225.30
- var val = num1.toPrecision(4);
- console.log(val)
- //1225
toFixed() 和 toPrecision() 转换的结果都是 string 类型,不是 number 类型
把数字转换为字符串,使用指定的基数。数字的基数是 2 ~ 36 之间的整数。
若省略该参数,则使用基数 10。
- var num1 = 1225.30
- var val = num1.toString();
- console.log(val)
- #1225.3
-
-
- var val1 = num1.toString(2);
- console.log(val1)
- 10011001001.010011001100110011001100110011001100110011
把数字转换为字符串,使用本地数字格式顺序。
- var num1 = 1225.30
- var val = num1.toString();
- console.log(val)
- //1,225.3
-
1.toLocaleString(),当数字是四位数及以上时,从右往左数,每三位用分号隔开,并且小数点后只保留三位;而toString()单纯将数字转换为字符串。
2.toLocaleString(),当目标是标准时间格式时,输出简洁年月日,时分秒;而toString()输出国际表述字符串。
- var num1 = new Date();
- var val = num1.toLocaleString();
- console.log(val)
- //2023/10/9 16:13:54
-
- var val1 = num1.toString();
- console.log(val1)
- //Mon Oct 09 2023 16:13:54 GMT+0800 (中国标准时间)