Number
JavaScript 的 Number
对象是经过封装的能让你处理数字值的对象。Number
对象由 Number() 构造器创建。
语法
new Number(value);
var a = new Number('123'); // a === 123 is false
var b = Number('123'); // b === 123 is true
a instanceof Number; // is true
b instanceof Number; // is false
Number
对象主要用于:
- 如果参数无法被转换为数字,则返回NaN。
- 在非构造器上下文中 (如:没有new操作符),
Number
能被用来执行类型转换。
整数类型的范围
-2^53
到2^53
之间(不含两个端点),超过这个范围,无法精确表示这个整数。
Number的一些属性
- Number.MAX_VALUE属性表示在 JavaScript 里所能表示的最大数值。值接近于1.79E+308。大于MAX_VALUE的值代表 "Infinity"。
- Number.MIN_VALUE属性表示在 JavaScript 中所能表示的最小的正值。是 JavaScript 里最接近 0 的正值,而不是最小的负值。约为 5e-324。小于MIN_VALUE的值将会转换为 0。
- Number.NEGATIVE_INFINITY属性表示负无穷大。Number.NEGATIVE_INFINITY 的值和全局对象的 Infinity 属性的负值相同
该值的行为同数学上的无穷大(infinity)有一点儿不同:
1、任何正值,包括POSITIVE_INFINITY,乘以NEGATIVE_INFINITY为NEGATIVE_INFINITY。
2、任何负值,包括NEGATIVE_INFINITY,乘以NEGATIVE_INFINITY为POSITIVE_INFINITY。
3、0 乘以NEGATIVE_INFINITY为NaN.
4、NaN 乘以NEGATIVE_INFINITY为NaN.
5、NEGATIVE_INFINITY除以任何负值(除了NEGATIVE_INFINITY)为POSITIVE_INFINITY。
6、NEGATIVE_INFINITY除以任何正值(除了POSITIVE_INFINITY)为NEGATIVE_INFINITY。
7、NEGATIVE_INFINITY除以NEGATIVE_INFINITY或POSITIVE_INFINITY是NaN。
8、任何数除以NEGATIVE_INFINITY为 0。 - Number.POSITIVE_INFINITY 属性表示正无穷大。Number.POSITIVE_INFINITY 的值同全局对象 Infinity 属性的值相同。
该值的表现同数学上的无穷大有点儿不同:
1、任何正值,包括 POSITIVE_INFINITY,乘以 POSITIVE_INFINITY 为 POSITIVE_INFINITY。
2、任何负值,包括 NEGATIVE_INFINITY,乘以 POSITIVE_INFINITY 为 NEGATIVE_INFINITY。
3、0 乘以 POSITIVE_INFINITY 为 NaN。
4、NaN 乘以 POSITIVE_INFINITY 为 NaN。
5、POSITIVE_INFINITY 除以 NEGATIVE_INFINITY 以外的任何负值为 NEGATIVE_INFINITY。
6、POSITIVE_INFINITY 除以 POSITIVE_INFINITY 以外的任何正值为 POSITIVE_INFINITY。
7、POSITIVE_INFINITY 除以 NEGATIVE_INFINITY 或 POSITIVE_INFINITY 为 NaN。
8、任何数除以 POSITIVE_INFINITY 为 0。 - Number.NaN 表示“非数字”(Not-A-Number)。和 NaN 相同。
console.log(Number.NEGATIVE_INFINITY) // -Infinity
console.log(-Number.NEGATIVE_INFINITY) // Infinity
console.log(Number.NEGATIVE_INFINITY + 100) // -Infinity
console.log(typeof Number.NEGATIVE_INFINITY) // number
console.log(Number.POSITIVE_INFINITY * Number.NEGATIVE_INFINITY) // -Infinity
console.log(Number.NEGATIVE_INFINITY * Number.NEGATIVE_INFINITY) // Infinity
console.log(0 * Number.NEGATIVE_INFINITY) // NaN
console.log(0 / Number.NEGATIVE_INFINITY) // -0
console.log(0 - Number.NEGATIVE_INFINITY) // Infinity
console.log(-1 - Number.NEGATIVE_INFINITY) // Infinity
console.log(Number.NaN / Number.NEGATIVE_INFINITY) // NaN
console.log(Number.NaN * Number.NEGATIVE_INFINITY) // NaN
console.log(Number.NaN + Number.NEGATIVE_INFINITY) // NaN
console.log(Number.NEGATIVE_INFINITY / Number.NaN) // NaN
console.log(Number.NEGATIVE_INFINITY * Number.NaN) // NaN
console.log(Number.NEGATIVE_INFINITY + Number.NaN) // NaN
console.log(Number.NEGATIVE_INFINITY / Number.NEGATIVE_INFINITY) // NaN
console.log(Number.NEGATIVE_INFINITY * Number.NEGATIVE_INFINITY) // Infinity
console.log(Number.NEGATIVE_INFINITY / -100) // Infinity
console.log(Number.NEGATIVE_INFINITY / Number.POSITIVE_INFINITY) // NaN
console.log(Number.NEGATIVE_INFINITY * Number.POSITIVE_INFINITY) // -Infinity
console.log(Number.NEGATIVE_INFINITY / 100) // -Infinity
console.log(1 / Number.NEGATIVE_INFINITY) // -0
Number的一些方法
- Number.isFinite()方法用来检测传入的参数是否是一个有穷数(finite number)。
返回值:一个布尔值,表示给定的值是否是一个有穷数。
和全局的isFinite()函数相比,这个方法不会强制将一个非数值的参数转换成数值,这就意味着,只有数值类型的值,且是有穷的(finite),才返回true。Number.isFinite(Infinity); // false Number.isFinite(NaN); // false Number.isFinite(-Infinity); // false Number.isFinite(0); // true Number.isFinite(2e64); // true Number.isFinite('0'); // false, 全局函数 isFinite('0') 会返回 true Number.isFinite(null) // false ==== // 全局的 isFinite(Infinity); // false isFinite(NaN); // false isFinite(-Infinity); // false isFinite(0); // true isFinite(2e64); // true isFinite("0"); // true, 在更强壮的Number.isFinite('0')中将会得到false isFinite(null) // true Number(null) == 0 返回的是true isFinite(false) // true
- Number.isInteger() 方法用来判断给定的参数是否为整数。
如果被检测的值是整数,则返回 true,否则返回 false。注意 NaN 和正负 Infinity 不是整数.Number.isInteger(-100000); // true Number.isInteger(0.1); // false Number.isInteger(Math.PI); // false Number.isInteger(Infinity); // false Number.isInteger("10"); // false Number.isInteger(false); // false Number.isInteger([1]); // false
- Number.isNaN() 方法确定传递的值是否为 NaN和其类型是 Number。它是原始的全局isNaN()的更强大的版本。
和全局函数 isNaN() 相比,该方法不会强制将参数转换成数字,只有在参数是真正的数字类型,且值为 NaN 的时候才会返回 true。Number.isNaN(NaN); // true Number.isNaN(Number.NaN); // true Number.isNaN(0 / 0) // true // 下面这几个如果使用全局的 isNaN() 时,会返回 true。 Number.isNaN("NaN"); // false,字符串 "NaN" 不会被隐式转换成数字 NaN。 Number.isNaN(undefined); // false Number.isNaN({}); // false Number.isNaN("blabla"); // false // 下面的都返回 false Number.isNaN(true); Number.isNaN(null); Number.isNaN(37); Number.isNaN("37"); Number.isNaN("37.37"); Number.isNaN(""); Number.isNaN(" ");
- Number.isSafeInteger() 方法用来判断传入的参数值是否是一个“安全整数”
安全整数范围为-(253 - 1)到
253 - 1
之间的整数,包含-(253 - 1)和
253 - 1
。Number.isSafeInteger(3); // true Number.isSafeInteger(Math.pow(2, 53)) // false Number.isSafeInteger(Math.pow(2, 53) - 1) // true Number.isSafeInteger(NaN); // false Number.isSafeInteger(Infinity); // false Number.isSafeInteger("3"); // false Number.isSafeInteger(3.1); // false Number.isSafeInteger(3.0); // true
- Number.prototype.toExponential(fractionDigits):以指数表示法返回该数值字符串表示形式。
1、fractionDigits:一个整数,用来指定小数点后有几位数字。默认情况下用尽可能多的位数来显示数字。
2、对数值字面量使用 toExponential() 方法,且该数值没有小数点和指数时,应该在该数值与该方法之间隔开一个空格,以避免点号被解释为一个小数点。也可以使用两个点号调用该方法。
3、如果一个数值的小数位数多余 fractionDigits 参数所提供的,则该数值将会在 fractionDigits 指定的小数位数处四舍五入。
4、如果 fractionDigits 太小或太大将会抛出该错误。介于 0 和 20(包括20)之间的值不会引起 RangeError 。 执行环境也可以支持更大或更小范围。var numObj = 77.1234; console.log("numObj.toExponential() is " + numObj.toExponential()); // 输出 7.71234e+1 console.log("numObj.toExponential(4) is " + numObj.toExponential(4)); // 输出 7.7123e+1 console.log("numObj.toExponential(2) is " + numObj.toExponential(2)); // 输出 7.71e+1 console.log("77.1234.toExponential() is " + 77.1234.toExponential()); // 输出 7.71234e+1 console.log("77 .toExponential() is " + 77 .toExponential()); // 输出 7.7e+1
- Number.prototype.toFixed(digits):使用定点表示法来格式化一个数值。该数值在必要时进行四舍五入,另外在必要时会用 0 来填充小数部分,以便小数部分有指定的位数。
1、digits:小数点后数字的个数;介于 0 到 20 (包括)之间,实现环境可能支持更大范围。如果忽略该参数,则默认为 0。
2、如果 digits 参数太小或太大。0 到 20(包括)之间的值不会引起RangeError。实现环境(implementations)也可以支持更大或更小的值
3、返回值:使用定点表示法表示给定数字的字符串。
4、 如果数值大于 1e+21,该方法会简单调用 Number.prototype.toString()并返回一个指数记数法格式的字符串。var numObj = 12345.6789 numObj.toFixed() // 返回 "12346":进行四舍五入,不包括小数部分 numObj.toFixed(1) // 返回 "12345.7":进行四舍五入 numObj.toFixed(6); // 返回 "12345.678900":用0填充 (1.23e+20).toFixed(2); // 返回 "123000000000000000000.00" (1.23e-10).toFixed(2) // 返回 "0.00" 2.34.toFixed(1); // 返回 "2.3" -2.34.toFixed(1); // 返回 -2.3 (由于操作符优先级,负数不会返回字符串) (-2.34).toFixed(1) // 返回 "-2.3" (若用括号提高优先级,则返回字符串)
- Number.prototype.toPrecision(precision):以指定的精度返回该数值对象的字符串表示。
1、precision:可选。一个用来指定有效数个数的整数。
2、以定点表示法或指数表示法表示的一个数值对象的字符串表示,四舍五入到 precision 参数指定的显示数字位数。
3、如果忽略 precision 参数,则该方法表现类似于 Number.prototype.toString()。如果该参数是一个非整数值,将会向下舍入到最接近的整数。
4、如果 precison 参数不在 1 和 100 (包括)之间,将会抛出一个RangeError
。执行环境也可以支持更大或更小的范围。ECMA-262 只需要最多 21 位显示数字。var numObj = 5.123456; console.log("numObj.toPrecision() is " + numObj.toPrecision()); //输出 5.123456 console.log("numObj.toPrecision(5) is " + numObj.toPrecision(5)); //输出 5.1235 console.log("numObj.toPrecision(2) is " + numObj.toPrecision(2)); //输出 5.1 console.log("numObj.toPrecision(1) is " + numObj.toPrecision(1)); //输出 5 // 注意:在某些情况下会以指数表示法返回 console.log((1234.5).toPrecision(2)); // "1.2e+3"
- Number.prototype.toString(radix):返回指定 Number 对象的字符串表示形式。
1、radix: 指定要用于数字到字符串的转换的基数(从2到36)。如果未指定 radix 参数,则默认值为 10。
2、如果 toString() 的 radix 参数不在 2 到 36 之间,将会抛出一个 RangeError。
3、Number 对象覆盖了 Object 对象上的 toString() 方法,它不是继承的 Object.prototype.toString()。对于 Number 对象,toString() 方法以指定的基数返回该对象的字符串表示。var count = 10; console.log(count.toString()); // 输出 '10' console.log((17).toString()); // 输出 '17' console.log((17.2).toString()); // 输出 '17.2' var x = 6; console.log(x.toString(2)); // 输出 '110' console.log((254).toString(16)); // 输出 'fe' console.log((-10).toString(2)); // 输出 '-1010' console.log((-0xff).toString(2)); // 输出 '-11111111'