This article documents how to work with the Number
built-in object, and lists its properties and methods.
本文介绍如何使用Number
内置对象,并列出其属性和方法。
A number
value can be generated using a number literal syntax:
可以使用数字文字语法生成number
值:
const age = 36
typeof age //number
or using the Number
global function:
或使用Number
全局功能:
const age = Number(36)
typeof age //number
If we add the new
keyword, we get a Number
object in return:
如果添加new
关键字,则会得到一个Number
对象作为回报:
const age = new Number(36)
typeof age //object
which has a very different behavior than a number
type. You can get the original number
value using the valueOf()
method:
与number
类型的行为截然不同。 您可以使用valueOf()
方法获取原始number
值:
const age = new Number(36)
typeof age //object
age.valueOf() //36
物产 (Properties)
EPSILON
the smallest interval between two numbersEPSILON
两个数字之间的最小间隔MAX_SAFE_INTEGER
the maximum integer value JavaScript can representMAX_SAFE_INTEGER
JavaScript可以表示的最大整数MAX_VALUE
the maximum positive value JavaScript can representMAX_VALUE
JavaScript可以代表的最大正值MIN_SAFE_INTEGER
the minimum integer value JavaScript can representMIN_SAFE_INTEGER
JavaScript可以表示的最小整数MIN_VALUE
the minimum positive value JavaScript can representMIN_VALUE
JavaScript可以代表的最小正值NaN
a special value representing “not a number”NaN
代表“不是数字”的特殊值NEGATIVE_INFINITY
a special value representing negative infinityNEGATIVE_INFINITY
代表负无穷大的特殊值POSITIVE_INFINITY
a special value representing positive infinityPOSITIVE_INFINITY
代表正无穷大的特殊值
Those properties evaluated to the values listed below:
这些属性评估为以下列出的值:
Number.EPSILON
Number.MAX_SAFE_INTEGER
Number.MAX_VALUE
Number.MIN_SAFE_INTEGER
Number.MIN_VALUE
Number.NaN
Number.NEGATIVE_INFINITY
Number.POSITIVE_INFINITY
2.220446049250313e-16
9007199254740991
1.7976931348623157e+308
-9007199254740991
5e-324
NaN
-Infinity
Infinity
对象方法 (Object Methods)
We can call those methods passing a value:
我们可以调用那些传递值的方法:
Number.isNaN(value)
: returns true ifvalue
is not a numberNumber.isNaN(value)
:如果value
不是数字,则返回trueNumber.isFinite(value)
: returns true ifvalue
is a finite numberNumber.isFinite(value)
:如果value
是一个有限数,则返回trueNumber.isInteger(value)
: returns true ifvalue
is an integerNumber.isInteger(value)
:如果value
是一个整数,则返回trueNumber.isSafeInteger(value)
: returns true ifvalue
is a safe integerNumber.isSafeInteger(value)
:如果value
是一个安全整数,则返回trueNumber.parseFloat(value)
: convertsvalue
to a floating point number and returns itNumber.parseFloat(value)
:将value
转换为浮点数并返回Number.parseInt(value)
: convertsvalue
to an integer and returns itNumber.parseInt(value)
:将value
转换为整数并返回
I mentioned “safe integer”. Also up above, with the MAX_SAFE_INTEGER and MIN_SAFE_INTEGER properties. What is a safe integer? It’s an integer that can be exactly represented as an IEEE-754 double precision number (all integers from (2^53 - 1) to -(2^53 - 1)). Out of this range, integers cannot be represented by JavaScript correctly. Out of the scope of the course, but here is a great explanation of that.
我提到了“安全整数”。 同样位于上方,具有MAX_SAFE_INTEGER和MIN_SAFE_INTEGER属性。 什么是安全整数? 它是一个整数,可以精确地表示为IEEE-754双精度数字(所有整数(从(2 ^ 53-1)到-(2 ^ 53-1)))。 超出此范围,JavaScript无法正确表示整数。 这超出了课程的范围,但这是对此的一个很好的解释 。
实例方法 (Instance methods)
When you use the new
keyword to instantiate a value with the Number() function, we get a Number
object in return:
当您使用new
关键字通过Number()函数实例化一个值时,我们得到一个Number
对象作为回报:
const age = new Number(36)
typeof age //object
This object offers a few unique methods you can use. Mostly to convert the number to specific formats.
该对象提供了一些可以使用的独特方法。 通常是将数字转换为特定格式。
.toExponential()
: return a string representing the number in exponential notation.toExponential()
:返回一个以指数表示形式表示数字的字符串.toFixed()
: return a string representing the number in fixed-point notation.toFixed()
:返回一个字符串,该字符串以定点表示法表示数字.toLocaleString()
: return a string with the local specific conventions of the number.toLocaleString()
:返回具有数字的本地特定约定的字符串.toPrecision()
: return a string representing the number to a specified precision.toPrecision()
:返回表示数字的字符串,达到指定的精度.toString()
: return a string representing the specified object in the specified radix (base). Overrides the Object.prototype.toString() method.toString()
:返回一个字符串,该字符串表示指定基数(基数)中的指定对象。 重写Object.prototype.toString()方法.valueOf()
: return the number primitive value of the object.valueOf()
:返回对象的数字原始值