TypeScript Numbers
TypeScript is a strict superset of JavaScript and extends the capabilities of numerical values through its Number type. In this article, we’ll explore TypeScript numbers, covering both integers and floating-point values.
Syntax:
var var_name = new Number(value)
Basics of TypeScript Numbers
1. Number Class and Wrapper:
- TypeScript treats numbers as both integers and floating-point values.
- The
Number
class acts as a wrapper, allowing manipulation of numeric literals as if they were objects.
2. Common Properties:
- MAX_VALUE: Represents the largest possible value in JavaScript (approximately 1.8 × 10^308).
- MIN_VALUE: Denotes the smallest positive value (approximately 5 × 10^-324).
- NaN: Indicates a value that is not a number.
- NEGATIVE_INFINITY: Represents a value less than MIN_VALUE.
- POSITIVE_INFINITY: Corresponds to a value greater than MAX_VALUE.
3. Methods:
- toExponential(): Converts a number to exponential notation.
- toFixed(digits): Formats the number with a specific number of decimal places.
- toLocaleString(): Converts the number into a locale-specific representation.
- toPrecision(totalDigits): Specifies the total digits (including both integer and decimal parts).
- toString(base): Returns the string representation of the number in the specified base.
- valueOf(): Retrieves the number’s primitive value.
Example 1: Maximum and Minimum Limits of Numbers in TypeScript
console.log("Number Properties in TypeScript:");
console.log("Maximum value of a number variable has :"
+ Number.MAX_VALUE);
console.log("The least value of a number variable has:"
+ Number.MIN_VALUE);
console.log("Value of Negative Infinity:"
+ Number.NEGATIVE_INFINITY);
console.log("Value of Negative Infinity:"
+ Number.POSITIVE_INFINITY);
Output
Number Properties in TypeScript: Maximum value of a number variable has :1.7976931348623157e+308 The least value of a number variable has:5e-324 Value of Negative Infinity:-Infinity Value of Negative ...
Example 2: NaN Value
var day = 0
if( day<=0 || day > 7) {
day = Number.NaN
console.log("Day is "+ day)
} else {
console.log("Value Accepted..")
}
Output
Day is NaN
Example 3: toExponential() – Converts a number to exponential notation.
// The toExponential()
var num1 = 2525.30
var val = num1.toExponential();
console.log(val)
Output
2.5253e+3
Example 4: toFixed() – Formats the number with a specific number of decimal places.
// The toFixed()
var num3 = 237.134
console.log("num3.toFixed() is "+num3.toFixed())
console.log("num3.toFixed(2) is "+num3.toFixed(3))
console.log("num3.toFixed(6) is "+num3.toFixed(5))
Output
num3.toFixed() is 237 num3.toFixed(2) is 237.134 num3.toFixed(6) is 237.13400
Example 5: toLocaleString() – Converts the number into a locale-specific representation.
// The toLocaleString()
var num = new Number( 237.1346);
console.log( num.toLocaleString());
Output
237.135
Example 6: toPrecision(): Specifies the total digits (including both integer and decimal parts).
// The toPrecision()
var num = new Number(5.7645326);
console.log(num.toPrecision());
console.log(num.toPrecision(1));
console.log(num.toPrecision(2));
Output
5.7645326 6 5.8
Example 7: toString(base): Returns the string representation of the number in the specified base.
// The toString()
var num = new Number(10);
console.log(num.toString());
console.log(num.toString(2));
console.log(num.toString(8));
Output
10 1010 12
Example 8: valueOf(): Retrieves the number’s primitive value.
// The valueOf()
var num = new Number(20);
console.log(num.valueOf());
Output
20
Contact Us