Numbers in TypeScript

Job-ready Online Courses: Click, Learn, Succeed, Start Now!

In TypeScript, numbers are a primitive data type used to represent numerical values. The number type can hold both integers and floating-point numbers.

TypeScript supports basic arithmetic operations, such as addition, subtraction, multiplication, and division. It also supports more advanced mathematical operations, such as trigonometric functions, logarithms, and rounding.

In addition to the number type, TypeScript supports other numerical types, such as BigInt, which can hold arbitrary length integers, and Math.Random, which provides a random number generator.

When working with numbers in TypeScript, knowing the potential issues related to precision and rounding errors is important. Using appropriate data types and functions is also important to ensure your code is correct and efficient.

Overall, TypeScript provides robust support for working with numerical data, including basic arithmetic operations, advanced mathematical functions, and support for various numerical types.

Basic Number Types in TypeScript

The four fundamental types of numbers in TypeScript are number, bigint, float, and double. The most used number type, represents a 64-bit floating-point value. This indicates that a wide range of values can be stored, including both extremely small and very large numbers. Here’s an illustration of how to use the number type:

let DataFlair_x: number = 42;
let DataFlair_y: number = 3.14;

The bigint type is a relatively new addition to TypeScript, introduced in version 3.2. It represents an integer value of arbitrary size. Here’s an example:

let DataFlair_big: bigint = 9007199254740991;

The float and double types represent floating-point numbers of 32 bits and 64 bits, respectively. Here’s an example of using the float type:

let DataFlair_a: float = 3.14159265359;

Number Literals in TypeScript

TypeScript also supports number literals, which allow you to specify a specific value for a number variable. Number literals can be written in decimal, binary, octal, and hexadecimal notation. Here’s an example of using a number literal in TypeScript:

let DataFlair_binary: number = 0b1010; // binary notation
let DataFlair_octal: number = 0o744; // octal notation
let DataFlair_decimal: number = 6; // decimal notation
let DataFlair_hexadecimal: number = 0x7b; // hexadecimal notation

NaN and Infinity in TypeScript

TypeScript also supports special numeric values, such as NaN and Infinity. NaN represents a value that is not a number, while Infinity represents a value that is infinitely large. Here’s an example:

let DataFlair_notANumber: number = NaN;
let DataFlair_infinity: number = Infinity;

MIN_VAL and MAX_VAL in TypeScript

In TypeScript, MIN_VAL and MAX_VAL are constants that represent the minimum and maximum values that can be stored in a number data type. These constants can be used to validate input or to define numeric boundaries in your code.

For example, you might use the MIN_VAL and MAX_VAL constants to validate that a user-entered number is within a certain range:

const DataFlair_MIN_VAL = -100;
const DataFlair_MAX_VAL = 100;

function validateInput(input: number): boolean {
 return input >= DataFlair_MIN_VAL && input <= DataFlair_MAX_VAL;
}

console.log(validateInput(50)); // true
console.log(validateInput(200)); // false

Output –

true
false

In addition to MIN_VAL and MAX_VAL, TypeScript also provides two special constants for representing positive and negative infinity. These constants are POSITIVE_INFINITY and NEGATIVE_INFINITY, respectively.

POSITIVE_INFINITY and NEGATIVE_INFINITY are constants that represent positive and negative infinity, respectively. These values are returned when a mathematical operation exceeds a number’s maximum or minimum representable value.

For example:

console.log(1 / 0); // Infinity
console.log(-1 / 0); // -Infinity

Output

Infinity
-Infinity

It is important to know these constants when working with numerical data in TypeScript to ensure that your code handles edge cases and potential overflow errors correctly.

Type Conversions in TypeScript

TypeScript also provides a way to convert between different numeric types. For example, you can convert a number to a bigint using the BigInt() function. Here’s an example:

let DataFlair_x: number = 42;
let DataFlair_big: bigint = BigInt(x);

Similarly, you can convert a number to a string using the toString() method:

let DataFlair_x: number = 42;
let DataFlair_big: string = DataFlair_x.toString();

Number Methods in TypeScript

TypeScript provides several built-in methods for working with numerical data. Some of the most commonly used methods include:

1. toFixed(n: number): string: Returns a string representation of the number with n decimal places.

2. toPrecision(n: number): string: Returns a string representation of the number with n significant figures.

3. toString(radix?: number): string: Returns a string representation of the number in the specified base (radix). The default is base 10.

4. isNaN(): boolean: Returns true if the number is NaN (not a number), false otherwise.

5. isFinite(): boolean: Returns true if the number is finite (i.e., not Infinity, -Infinity, or NaN), false otherwise.

6. parseFloat(str: string): number: Parses a string and returns the floating-point number it represents.

7. parseInt(str: string, radix?: number): number: Parses a string and returns the integer it represents in the specified base (radix). The default is base 10.

For example:

const DataFlair_num = 3.14159;


console.log(DataFlair_num.toFixed(2)); // "3.14"
console.log(DataFlair_num.toPrecision(3)); // "3.14"
console.log(DataFlair_num.toString(16)); // "3.243f6a8885a3"
console.log(isNaN(DataFlair_num)); // false
console.log(isFinite(DataFlair_num)); // true
console.log(parseFloat('3.14159')); // 3.14159
console.log(parseInt('1010', 2)); // 10

Output

3.14
3.14
3.243f3e037
false
true
3.14159
10

These methods provide a convenient way to work with numerical data in TypeScript and can be used to perform a wide range of mathematical operations and conversions.

Conclusion

Strong tools for working with numbers are provided by TypeScript, such as support for diverse numeric types, number literals, unique numeric values, and type conversions. By utilizing TypeScript’s robust type features, you may develop more dependable code and catch problems earlier.

Your opinion matters
Please write your valuable feedback about DataFlair on Google

courses

DataFlair Team

DataFlair Team provides high-impact content on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. We make complex concepts easy to grasp, helping learners of all levels succeed in their tech careers.

Leave a Reply

Your email address will not be published. Required fields are marked *