Open In App

TypeScript Numbers

Last Updated : 09 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

TypeScript Numbers refer to the numerical data type in TypeScript, encompassing integers and floating-point values. The Number class in TypeScript provides methods and properties for manipulating these values, allowing for precise arithmetic operations and formatting, enhancing JavaScript’s native number handling.

Syntax:

let 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

In this example we are showing several Number properties in TypeScript: MAX_VALUE (the largest number), MIN_VALUE (the smallest positive number), NEGATIVE_INFINITY, and POSITIVE_INFINITY, showing TypeScript’s numeric limits and infinity values.

javascript
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

In this example we checks if day is outside the range 1-7. If so, it sets day to NaN and logs it; otherwise, it logs Value Accepted.

javascript
let day: number = 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.

javascript
// The toExponential() 
let num1: number = 2525.30;
let val: string = num1.toExponential();
console.log(val);

Output
2.5253e+3

Example 4: toFixed() – Formats the number with a specific number of decimal places.

javascript
// The toFixed()
let num3: number = 237.134;
console.log("num3.toFixed() is " + num3.toFixed());
console.log("num3.toFixed(2) is " + num3.toFixed(2));
console.log("num3.toFixed(6) is " + num3.toFixed(6));

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.

javascript
// The toLocaleString()
let num: Number = new Number(237.1346);
console.log(num.toLocaleString());

Output
237.135

Example 6: toPrecision(): Specifies the total digits (including both integer and decimal parts).

javascript
// The toPrecision()
let num: Number = 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.

javascript
// The toString()
let num: Number = 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.

javascript
// The valueOf()
let num: Number = new Number(20);
console.log(num.valueOf());

Output
20

Conclusion

In this article, we explored how TypeScript enhances the handling of numerical values through its Number type. We discussed the Number class, its properties, and methods that provide powerful tools for manipulating and formatting numbers. By leveraging these capabilities, developers can write more precise and expressive numerical code, ensuring robustness and clarity in TypeScript applications. Understanding these features allows you to take full advantage of TypeScript’s number handling, leading to cleaner, more efficient code.



Next Article

Similar Reads