TypeScript Number
TypeScript, similar to JavaScript, supports the Number object.
The Number object is a wrapper object for primitive numeric values.
Syntax
var num = new Number(value);
Note: If a parameter value cannot be converted to a number, it returns NaN (Not-a-Number).
Number Object Properties
The following table lists the properties supported by the Number object:
No. | Property & Description |
---|---|
1. | MAX_VALUE The largest number that can be represented. The MAX_VALUE property is close to 1.79E+308. Values greater than MAX_VALUE are represented as "Infinity". |
2. | MIN_VALUE The smallest number that can be represented, the closest positive number to zero (which will not actually become zero). The largest negative number is -MIN_VALUE. The value of MIN_VALUE is approximately 5e-324. Values smaller than MIN_VALUE ("underflow values") are converted to 0. |
3. | NaN Not-a-Number value. |
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 the object. |
7. | constructor Returns a reference to the Number function that created the 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);
Compiling the above code generates 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);
Output:
TypeScript Number Properties:
Maximum value: 1.7976931348623157e+308
Minimum value: 5e-324
Negative infinity: -Infinity
Positive infinity: Infinity
NaN Example
TypeScript
var month = 0
if( month<=0 || month >12) {
month = Number.NaN
console.log("Month is: " + month)
} else {
console.log("Correct month value entered.")
}
Compiling the above code generates the following JavaScript code:
JavaScript
var month = 0;
if (month <= 0 || month > 12) {
month = Number.NaN;
console.log("Month is: " + month);
}
else {
console.log("Correct month value entered.");
}
Output:
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 = "[email protected]"
console.log("Employee ID: " + emp.id)
console.log("Employee Name: " + emp.name)
console.log("Employee Email: " + emp.email)
Compiling the above code generates 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);
Output:
Employee ID: 123
Employee Name: admin
Employee Email: [email protected]
Number Object Methods
The Number object supports the following methods:
No. | Method & Description | Example |
---|---|---|
1. | toExponential() Converts the value of the object to exponential notation. | // toExponential() <br>var num1 = 1225.30 <br>var val = num1.toExponential(); <br>console.log(val) // Output: 1.2253e+3 |
2. | toFixed() Converts a number to a string, rounding to a specified number of decimals. | var num3 = 177.234 <br>console.log("num3.toFixed() is " + num3.toFixed()) // Output: 177<br>console.log("num3.toFixed(2) is " + num3.toFixed(2)) // Output: 177.23<br>console.log("num3.toFixed(6) is " + num3.toFixed(6)) // Output: 177.234000 |
3. | toLocaleString() Converts a number to a string, using local number formatting. | var num = new Number(177.1234); <br>console.log(num.toLocaleString()); // Output: 177.1234 |
4. | toPrecision() Formats a number to a specified length. | var num = new Number(7.123456); <br>console.log(num.toPrecision()); // Output: 7.123456 <br>console.log(num.toPrecision(1)); // Output: 7<br>console.log(num.toPrecision(2)); // Output: 7.1 |
5. | toString() Converts a number to a string, using the specified base. The base is an integer between 2 and 36. If omitted, base 10 is used. | var num = new Number(10); <br>console.log(num.toString()); // Output in base 10: 10<br>console.log(num.toString(2)); // Output in base 2: 1010<br>console.log(num.toString(8)); // Output in base 8: 12 |
6. | valueOf() Returns the primitive value of a Number object. | var num = new Number(10); <br>console.log(num.valueOf()); // Output: 10 |