TypeScript Numbers: Exploring TypeScript Data Type “Numbers”

What is a TypeScript number?

A number in TypeScript is a basic data type that represents numeric values.

Here’s an easier way to understand it:

How to Declare:

You can create a number variable like the following:

let age: number = 18;


Arithmetic Operations:

Arithmetic operations can be performed with numbers.

For instance:

let sum: number = x + y;

Increment and Decrement:

The increment (++) and decrement (–) operators can be used to change number variables.

TypeScript Numbers Common properties

The following are the common properties of the number object:

  1. MAX_VALUE

The MAX_VALUE represents the largest possible numeric value.

console.log(Number.MAX_VALUE); 

Output:

1.7976931348623157e+308 

  1. MIN_VALUE

The MIN_VALUE is the smallest possible positive value.

console.log(Number.MIN_VALUE)

Output:

5e-324

  1. NaN

It refers to a value that is not a number.

console.log(Number.NaN); // Outputs: NaN
console.log(isNaN('itsourcecode')); // Outputs: true
console.log(isNaN(143)); // Outputs: false

  1. NEGATIVE_INFINITY

The NEGATIVE_INFINITY represents a value that is less than MIN_VALUE.

console.log(Number.NEGATIVE_INFINITY); 
console.log(-1 / 0);

Output:

-Infinity
-Infinity

  1. POSITIVE_INFINITY

The POSITIVE_INFINITY is a value that is greater than MAX_VALUE.

console.log(Number.POSITIVE_INFINITY); 
console.log(1 / 0); 

Output:

Infinity
Infinity

  1. Prototype

The Number object has a static property.
You can use the prototype property to add new properties and methods to the Number object in the current document.

Constructor

This refers to the function that was used to create the instance of this object. Typically, this is the Number object.

Number Methods

TypeScript offers several methods for working with numbers:

1. toFixed()

The toFixed() method formats a number with a specific number of decimal places and returns it as a string.

Syntax:

numObj.toFixed([digits])

For example:

let num = 12.3456789;
console.log(num.toFixed(3));

Output:

12.346

2. toExponential()

The toExponential() method returns a string representation of a number in exponential notation.

Syntax:

numObj.toExponential([fractionDigits])

For example:

let num = 1020304050;
console.log(num.toExponential()); 

Output:

1.02030405e+9

3. toLocaleString()

The toLocaleString() method formats the number into a local-specific representation of a number.

Syntax:

numObj.toLocaleString([locales [, options]])

For example:

let num = 102030405060.708090;
console.log(num.toLocaleString());

Output:

102,030,405,060.708

4. toPrecision()

The toPrecision() method returns a string representation of a number using the specified precision.

Syntax:

numObj.toPrecision([precision])

For example:

let num = 1.23456789;
console.log(num.toPrecision(4)); 

Output:

1.235

5. toString()

The toString() method converts a number to its string representation.

Syntax:

numObj.toString([radix])

For example:

let num = 102030;
console.log(num.toString()); 

Output:

102030

6. valueOf()

The valueOf() method returns the primitive value of the number.

Syntax:

numObj.valueOf()

For example:

let num = new Number(10203040);
console.log(num.valueOf()); 

Output:

10203040

7. isNaN()

The isNaN() method checks if a number is “Not-a-Number” (NaN).

Syntax:

isNaN(value)

For example:

console.log(isNaN(Number('hello'))); // Outputs: true
console.log(isNaN(Number(123))); // Outputs: false

8. isFinite()

The isFinite() method checks if a number is finite.

Syntax:

isFinite(testValue)

For example:

console.log(isFinite(143)); // Outputs: true
console.log(isFinite(Infinity)); // Outputs: false

9. parseFloat()

The parseFloat() method parses a string and returns a floating-point number.

Syntax:

parseFloat(value)

For example:

console.log(parseFloat("1.0230405060")); 

Output:

1.023040506

Conclusion

TypeScript is an enhanced version of JavaScript that simplifies working with numbers.
We’ve finished discussing how to use numbers in TypeScript, including both whole numbers and decimal numbers.

I hope this article has helped you understand the TypeScript Data Type “Numbers”.
If you have any questions or inquiries, please feel free to leave a comment below.

Leave a Comment