TypeScript Number
Similar to JavaScript, TypeScript supports the Number object.
In TypeScript, the Number object is used to wrap numeric values.
The Number object is a wrapper object for primitive numeric values.
Similar to the String object, the Number object is a reference type and differs from the primitive number type.
Although the Number object provides some additional properties and methods, in TypeScript it is more recommended to directly use the primitive number type, because the Number object brings performance overhead and type confusion.
Syntax
var num = new Number(value);
Note that this creates a reference-type object rather than a primitive number value.
Note: If an argument value cannot be converted to a number, NaN (Not a Number) is returned.
The difference between the Number object and the primitive number type
- Basic types
number: A primitive data type used to store numeric values. Numberobject: A reference type, a wrapper object used to wrap primitive numeric values.
Example
let numLiteral
:
number
=
42
;
let numObject
:
Number
=
new
Number
(
42
)
;
console.
log
(
typeof
numLiteral
)
;
// Output: "number"
console.
log
(
typeof
numObject
)
;
// Output: "object"
Number object properties
The following table lists the properties supported by the Number object:
| No. | Attribute & Description |
|---|---|
| 1. |
MAX_VALUE The largest number that can be represented; the MAX_VALUE property value is close to 1.79E+308. Values greater than MAX_VALUE represent "Infinity". |
| 2. |
MIN_VALUE The smallest number that can be represented, that is, the positive number closest to 0 (it will not actually become 0). The largest negative number is -MIN_VALUE, and the value of MIN_VALUE is about 5e-324. Values smaller than MIN_VALUE ("underflow values") will be converted to 0. |
| 3. |
NaN Not-A-Number value. |
| 4. |
NEGATIVE_INFINITY Negative infinity, returned when overflow occurs. This value is less than MIN_VALUE. |
| 5. |
POSITIVE_INFINITY Positive infinity, returned when overflow occurs. This value is greater than MAX_VALUE. |
| 6. |
prototype A static property of the Number object. It gives you the ability to add properties and methods to an object. |
| 7. |
constructor Returns a reference to the Number function that created this object. |
TypeScript
console
.
log
(
"
TypeScript Number Properties:
"
)
;
console
.
log
(
"
Maximum value:
"
Number
.
MAX_VALUE
)
;
console
.
log
(
"
Minimum value:
"
Number
.
MIN_VALUE
)
;
console
.
log
(
"
Negative infinity:
"
Number
.
NEGATIVE_INFINITY
)
;
console
.
log
(
"
Positive infinity:
"
Number
.
POSITIVE_INFINITY
)
;
Compile the above code to get the following JavaScript code:
JavaScript
console
.
log
(
"
TypeScript Number Properties:
"
)
;
console
.
log
(
"
Maximum value:
"
Number
.
MAX_VALUE
)
;
console
.
log
(
"
Minimum value:
"
Number
.
MIN_VALUE
)
;
console
.
log
(
"
Negative infinity:
"
Number
.
NEGATIVE_INFINITY
)
;
console
.
log
(
"
Positive infinity:
"
Number
.
POSITIVE_INFINITY
)
;
The output is:
TypeScript Number 属性:
最大值为: 1.7976931348623157e+308
最小值为: 5e-324
负无穷大: -Infinity
正无穷大:Infinity
NaN instance
TypeScript
var
month
=
0
if
(
month
<=
0
||
month
12
)
{
month
=
Number
.
NaN
console
.
log
(
"
The month is:
"
month
)
}
else
{
console
.
log
(
"
The month value entered is correct.
"
)
}
Compile the above code to get the following JavaScript code:
JavaScript
var
month
=
0
;
if
(
month
<=
0
||
month
12
)
{
month
=
Number
.
NaN
;
console
.
log
(
"
The month is:
"
month
)
;
}
else
{
console
.
log
(
"
The month value entered is correct.
"
)
;
}
The output is:
月份是:NaN
prototype instance
TypeScript
function
employee
(
id
:
number
,
name
:
string
)
{
this
.
id
=
id
this
.
name
=
name
}
var
emp
=
new
employee
(
123
,
"
admin
"
)
employee
.
prototype
.
=
"
admin@runoob.com
"
console
.
log
(
"
Employee ID:
"
emp
.
id
)
console
.
log
(
"
Employee name:
"
emp
.
name
)
console
.
log
(
"
Employee email:
"
emp
.
)
Compile the above code to get the following JavaScript code:
JavaScript
function
employee
(
id
,
name
)
{
this
.
id
=
id
;
this
.
name
=
name
;
}
var
emp
=
new
employee
(
123
,
"
admin
"
)
;
employee
.
prototype
.
=
"
admin@runoob.com
"
;
console
.
log
(
"
Employee ID:
"
emp
.
id
)
;
console
.
log
(
"
Employee name:
"
emp
.
name
)
;
console
.
log
(
"
Employee email:
"
emp
.
)
;
The output is:
员工号: 123
员工姓名: admin
员工邮箱: admin@runoob.com
Number object methods
The Number object supports the following methods:
| No. | Method & Description | Example |
|---|---|---|
| 1. | toExponential()
Converts the value of an object to exponential notation. |
//toExponential() var num1 = 1225.30 var val = num1.toExponential(); console.log(val) // Output: 1.2253e+3 | | 2. | toFixed()
Converts a number to a string and specifies the number of digits after the decimal point.
|var num3 = 177.234
console.log("num3.toFixed() 为 "+num3.toFixed()) // 输出:177
console.log("num3.toFixed(2) 为 "+num3.toFixed(2)) // 输出:177.23
console.log("num3.toFixed(6) 为 "+num3.toFixed(6)) // 输出:177.234000
| | 3. | toLocaleString()
Converts a number to a string using the local number format.
|var num = new Number(177.1234); console.log( num.toLocaleString()); // 输出:177.1234| | 4. | toPrecision()
Formats a number to a specified length.
|var num = new Number(7.123456); console.log(num.toPrecision()); // 输出:7.123456 console.log(num.toPrecision(1)); // 输出:7 console.log(num.toPrecision(2)); // 输出:7.1| | 5. | toString()
Converts a number to a string using the specified radix. The radix is an integer between 2 ~ 36. If this parameter is omitted, radix 10 is used.
|var num = new Number(10); console.log(num.toString()); // 输出10进制:10 console.log(num.toString(2)); // 输出2进制:1010 console.log(num.toString(8)); // 输出8进制:12| | 6. | valueOf()
Returns the primitive numeric value of a Number object.
|var num = new Number(10); console.log(num.valueOf()); // 输出:10|
Recommendations for using the Number object
In TypeScript, it is generally more recommended to use the primitive number type rather than the Number object. The reasons are as follows:
- Performance: Primitive types are lighter and perform better.
- Type consistency: TypeScript's type system prefers primitive types; using
Numberobjects may cause unexpected type mismatches. - Best Practices: The primitive type's
numberis more in line with TypeScript best practices, avoiding the unnecessary complexity brought by object wrapping.
If you really need to use specific methods of the Number object, you can convert the Number object to the primitive number type through the valueOf() method.
In short, TypeScript recommends using the primitive type number rather than the Number object, to keep code concise, efficient, and consistent.
Example
let numLiteral
:
number
=
123.456
;
let numObject
:
Number
=
new
Number
(
123.456
)
;
console.
log
(
numLiteral.
toFixed
(
2
)
)
;
// Output: "123.46"
console.
log
(
numObject.
valueOf
(
)
)
;
// Output: 123.456