DEV Community

Bogdan Galin
Bogdan Galin

Posted on

All you should know about numbers in TypeScript. Tutorial

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

let age : number = 30;
let price : number = 19.9;
Enter fullscreen mode Exit fullscreen mode

2. Common Operations

Arithmetic Operations

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
Enter fullscreen mode Exit fullscreen mode

Increment and Decrement

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

let count: number = 0;

count++; // Increment by 1 (count is now 1)
count--; // Decrement by 1 (count is back to 0)
Enter fullscreen mode Exit fullscreen mode

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:

let num: number = 42.123456;
let formattedNum: string = num.toFixed(2); // "42.12"
Enter fullscreen mode Exit fullscreen mode

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.

let num: number = 12345.6789;
let exponentialNum: string = num.toExponential(2);
console.log(exponentialNum); // Output: "1.23e+4"
Enter fullscreen mode Exit fullscreen mode

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.

let num: number = 123.456789;
let precisionNum: string = num.toPrecision(4);
console.log(precisionNum); // Output: "123.5"
Enter fullscreen mode Exit fullscreen mode

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.

let num: number = 42;
let binaryNum: string = num.toString(2); // Binary representation
console.log(binaryNum); // Output: "101010"
Enter fullscreen mode Exit fullscreen mode

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.

let value: number = NaN;
console.log(isNaN(value)); // Output: true
Enter fullscreen mode Exit fullscreen mode

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.

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

let infinityNum: number = Infinity;
console.log(isFinite(infinityNum)); // Output: false
Enter fullscreen mode Exit fullscreen mode

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.

let numStr: string = "3.14";
let parsedNum: number = parseFloat(numStr);
console.log(parsedNum); // Output: 3.14
Enter fullscreen mode Exit fullscreen mode

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.

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
Enter fullscreen mode Exit fullscreen mode

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

Top comments (0)