TypeScript Number

TypeScript, similar to JavaScript, supports the Number object.

In TypeScript, the Number object is used to wrap numeric types.

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, it is recommended to use the primitive number type directly in TypeScript, because the Number object incurs performance overhead and type confusion.

Syntax

var num = new Number(value);

Note that this creates an object of reference type, not a primitive number type.

Note:If a parameter value cannot be converted to a number, it will return NaN (Not-A-Number).

Difference between Number object and primitive number type

  • Primitive typenumber: primitive data type, used to store numeric values.
  • NumberObject: 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. Property & Description
1.

MAX_VALUE

The largest representable number. The MAX_VALUE property value is close to 1.79E+308. Values greater than MAX_VALUE represent "Infinity".

2.

MIN_VALUE

The smallest representable number, i.e., the positive number closest to 0 (it does not actually become 0). The largest negative number is -MIN_VALUE, and MIN_VALUE is approximately 5e-324. Values smaller than MIN_VALUE ("underflow values") will be converted to 0.

3.

NaN

Non-numeric value (Not-A-Number).

4.

NEGATIVE_INFINITY

Negative infinity, returned on overflow. This value is less than MIN_VALUE.

5.

POSITIVE_INFINITY

Positive infinity, returned on overflow. This value is greater than MAX_VALUE.

6.

prototype

Static property of the Number object. It allows you to add properties and methods to objects.

7.

constructor

Returns a reference to the Number function that created this object.

TypeScript

console.log("TypeScript Number properties:"); console.log("Maximum value is:" + Number.MAX_VALUE); console.log("Minimum value is:" + Number.MIN_VALUE); console.log("Negative infinity:" + Number.NEGATIVE_INFINITY); console.log("Positive infinity:" + Number.POSITIVE_INFINITY);

Compiling the above code produces the following JavaScript code:

JavaScript

console.log("TypeScript Number properties:"); console.log("Maximum value is:" + Number.MAX_VALUE); console.log("Minimum value is:" + 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 example

TypeScript

var month = 0 if( month<=0 || month >12) { month = Number.NaN console.log("Month is:"+ month) } else { console.log("The input month value is correct.") }

Compiling the above code produces the following JavaScript code:

JavaScript

var month = 0; if (month <= 0 || month > 12) { month = Number.NaN; console.log("Month is:" + month); } else { console.log("The input month value is correct."); }

The output is:

月份是:NaN

prototype example

TypeScript

function employee(id:number,name:string) { this.id = id this.name = name } var emp = new employee(123,"admin") employee.prototype.email = "[email protected]" console.log("Employee ID:"+emp.id) console.log("Employee name:"+emp.name) console.log("Employee email:"+emp.email)

Compiling the above code produces the following JavaScript code:

JavaScript

function employee(id, name) { this.id = id; this.name = name; } var emp = new employee(123, "admin"); employee.prototype.email = "[email protected]"; console.log("Employee ID:" + emp.id); console.log("Employee name:" + emp.name); console.log("Employee email:" + emp.email);

The output is:

员工号: 123
员工姓名: admin
员工邮箱: [email protected]

Number object methods

The Number object supports the following methods:

No. Method & Description Example
1. toExponential()

Converts the object's value to exponential notation.

//toExponential() 
var num1 = 1225.30 
var val = num1.toExponential(); 
console.log(val) // 输出: 1.2253e+3
2. toFixed()

Converts a number to a string, specifying the number of decimal places.

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 order.

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 and 36. If this parameter is omitted, base 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 Number object

In TypeScript, it is generally recommended to use the primitive number type rather than the Number object. The reasons are as follows:

  • Performance: Primitive types are more lightweight and have better performance.
  • Type consistency: TypeScript's type system favors primitive types, and usingNumberobjects may cause unexpected type mismatches.
  • Best practices: The primitive type isnumbermore in line with TypeScript best practices, avoiding unnecessary complexity caused by object wrapping.

If you really need to use specific methods of the Number object, you can convert the Number object to a primitive number type via the valueOf() method.

In summary, TypeScript recommends using the primitive number type rather than the Number object to keep the 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
Other extensions