All you should know about numbers in TypeScript. Tutorial
Bogdan Galin
Posted on September 4, 2023
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;
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
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)
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"
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"
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"
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"
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
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
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
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
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
Posted on September 4, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.