All you should know about numbers in TypeScript. Tutorial
Tags: typescript, webdev, node, tutorial
Published: 2023-09-04 10:41:15

Numbers are a fundamental data type in TypeScript, allowing you to work with numeric values in your programs. Understanding how to declare, manipulate, and format numbers is crucial for writing effective and accurate TypeScript code. Additionally, TypeScript's type system helps catch type-related errors during development, making your code more robust and reliable.

**1. Numbers**

```typescript
let age : number = 30;
let price : number = 19.9;
```

**2. Common Operations**

_Arithmetic Operations_

```typescript
let x: number = 10;
let y: number = 5;

let sum: number = x + y; // 15
let difference: number = x - y; // 5
let product: number = x * y; // 50
let quotient: number = x / y; // 2
```

_Increment and Decrement_

You can use the increment (++) and decrement (--) operators to modify number variables:

```typescript
let count: number = 0;

count++; // Increment by 1 (count is now 1)
count--; // Decrement by 1 (count is back to 0)
```

**3. Number Methods**

> toFixed(digits: number)

The _toFixed_ method allows you to format a number with a fixed number of decimal places and returns it as a string:

```typescript
let num: number = 42.123456;
let formattedNum: string = num.toFixed(2); // "42.12"
```

> toExponential(fractionDigits?: number): string

The _toExponential_ method returns a string representation of a number in exponential notation. The _fractionDigits_ parameter, which is optional, specifies the number of digits to include after the decimal point.

```typescript
let num: number = 12345.6789;
let exponentialNum: string = num.toExponential(2);
console.log(exponentialNum); // Output: "1.23e+4"
```

> toPrecision(precision: number): string

The _toPrecision_ method returns a string representation of a number using the specified precision. The _precision_ parameter determines the total number of significant digits in the result.

```typescript
let num: number = 123.456789;
let precisionNum: string = num.toPrecision(4);
console.log(precisionNum); // Output: "123.5"
```

> toString(radix?: number): string

The _toString_ method converts a number to its string representation. The _radix_ parameter, which is optional, specifies the base for representing the number. By default, it's base 10.

```typescript
let num: number = 42;
let binaryNum: string = num.toString(2); // Binary representation
console.log(binaryNum); // Output: "101010"
```

> isNaN(): boolean

The _isNaN_ method checks if a number is "Not-a-Number" (NaN). It returns _true_ if the value is NaN, and _false_ otherwise.

```typescript
let value: number = NaN;
console.log(isNaN(value)); // Output: true
```

> isFinite(): boolean

The _isFinite_ method checks if a number is finite (i.e., not NaN, Infinity, or -Infinity). It returns _true_ for finite numbers and _false_ for NaN or infinity values.

```typescript
let finiteNum: number = 42;
console.log(isFinite(finiteNum)); // Output: true

let infinityNum: number = Infinity;
console.log(isFinite(infinityNum)); // Output: false
```

> parseFloat(string: string): number

The _parseFloat_ function parses a string and returns a floating-point number. It stops parsing as soon as it encounters an invalid character.

```typescript
let numStr: string = "3.14";
let parsedNum: number = parseFloat(numStr);
console.log(parsedNum); // Output: 3.14
```

> parseInt(string: string, radix?: number): number

The _parseInt_ function parses a string and returns an integer. The optional _radix_ parameter specifies the base for parsing. If omitted, it assumes base 10.

```typescript
let numStr: string = "42";
let parsedInt: number = parseInt(numStr);
console.log(parsedInt); // Output: 42

let binaryStr: string = "1010";
let parsedBinary: number = parseInt(binaryStr, 2); // Parse as binary
console.log(parsedBinary); // Output: 10
```

These methods and properties associated with number values in TypeScript allow you to perform various operations and format numbers as needed in your applications. Understanding these functions is crucial for working with numeric data effectively.

Lots of useful knowledge on types: **How to calculate the 10,000th number in less than a second**, **How to make a server on the Express framework**, **Analysis of problems with Codewars\Leetcode**. You can get [here](https://boosty.to/npzss)