Ts 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, which is different from the primitive number type.
Although the Number object provides some additional properties and methods, it is generally recommended to use the primitive number type directly in TypeScript, as using the Number object can introduce performance overhead and type confusion.
### Syntax
var num = new Number(value);
Note that this creates a reference type object, not a primitive number type.
**Note:** If the parameter value cannot be converted to a number, it will return NaN (Not-A-Number).
### Difference Between Number Object and Primitive number Type
* **Primitive type `number`**: A primitive data type used to store numeric values.
* **`Number` object**: 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. | Property & Description |
| --- | --- |
| 1. | **MAX_VALUE** The largest representable positive number. The value of MAX_VALUE is approximately 1.79E+308. Values greater than MAX_VALUE represent "Infinity". |
| 2. | **MIN_VALUE** The smallest representable positive number, i.e., the positive number closest to 0 (but not actually 0). The largest negative number is -MIN_VALUE. The value of MIN_VALUE is approximately 5e-324. Values smaller than MIN_VALUE ("underflow values") will be converted to 0. |
| 3. | **NaN** 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** A static property of the Number object. Allows you 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 property: "); 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 yields the following JavaScript code:
## JavaScript
console.log("TypeScript Number property: "); 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 property:Maximum value is: 1.7976931348623157e+308Minimum value is: 5e-324Negative Infinity: -InfinityPositive Infinity:Infinity
### NaN Example
## TypeScript
var month = 0 if(month12){month = Number.NaN console.log("Month is: "+ month)}else{console.log("Month number input is correct.")}
Compiling the above code yields the following JavaScript code:
## JavaScript
var month = 0; if(month12){month = Number.NaN; console.log("Month is: " + month); }else{console.log("Month number input is correct."); }
The output is:
Month 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 = "admin@"console.log("Employee ID: "+emp.id)console.log("Employee Name: "+emp.name)console.log("Employee Email: "+emp.email)
Compiling the above code yields the following JavaScript code:
## JavaScript
function employee(id, name){this.id = id; this.name = name; }var emp = new employee(123, "admin"); employee.prototype.email = "admin@"; console.log("Employee ID: " + emp.id); console.log("Employee Name: " + emp.name); console.log("Employee Email: " + emp.email);
The output is:
Employee ID: 123Employee Name: admin Employee Email: admin@
* * *
## 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) // Output: 1.2253e+3 |
| 2. | toFixed() Converts a number to a string, keeping a specified number of decimals. | var num3 = 177.234 console.log("num3.toFixed() is "+num3.toFixed()) // Output:177 console.log("num3.toFixed(2) is "+num3.toFixed(2)) // Output:177.23 console.log("num3.toFixed(6) is "+num3.toFixed(6)) // Output:177.234000 |
| 3. | toLocaleString() Converts a number to a string, using local format conventions. | var num = new Number(177.1234); console.log( num.toLocaleString()); // Output:177.1234 |
| 4. | toPrecision() Formats a number to a specified length. | var num = new Number(7.123456); console.log(num.toPrecision()); // Output:7.123456 console.log(num.toPrecision(1)); // Output:7 console.log(num.toPrecision(2)); // Output:7.1 |
| 5. | toString() Converts a number to a string, using the specified radix. The radix is an integer between 2 and 36. If omitted, the radix defaults to 10. | var num = new Number(10); console.log(num.toString()); // Output in base 10:10 console.log(num.toString(2)); // Output in base 2:1010 console.log(num.toString(8)); // Output in base 8:12 |
| 6. | valueOf() Returns the primitive numeric value of a Number object. | var num = new Number(10); console.log(num.valueOf()); // Output:10 |
### Recommendations for Using the Number Object
In TypeScript, it is generally recommended to use the primitive number type instead of the Number object. Reasons include:
* **Performance**: Primitive types are lighter and offer better performance.
* **Type Consistency**: TypeScript's type system favors primitive types. Using the `Number` object can lead to unexpected type mismatches.
* **Best Practices**: Using the primitive `number` type aligns better with TypeScript best practices, avoiding the unnecessary complexity introduced by object wrappers.
If you do need to use a specific method of the Number object, you can convert the Number object to a primitive number type using the valueOf() method.
In summary, TypeScript recommends using the primitive number type over the Number object to maintain code simplicity, efficiency, and consistency.
## 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
YouTip