☰ See All Chapters |
TypeScript number type
Both integer and floating-point values have number type in TypeScript. There is no different data types for integers and floating-point. Interger value can either negartive or positive. All the values with number type will be treated as floating point value. Value can be in any of the below format.
Format | Example |
Integer | let postivateNum: number = 10; let negativeNum: number = -10; |
Floating point | let xyz: number = 1.2756; |
Exponential | let exponentialNum: number = 3e-2; |
Hexadecimal | let hexadecimalNum: number = 0x20; |
Octal | let octalNum: number = 0o20; |
Binary | let octalNum: number = 0b10000; |
TypeScript provides special number values that can be used in code. Five of them are given as follows:
Value | Description |
Number.NaN | Not a number - Returned when numeric operations applied on non numeric value. |
Number.POSITIVE_INFINITY | Returned when try to store value greater than max value. |
Number.POSITIVE_INFINITY | Returned when try to store value lesser than min value (negative value). |
Number.MIN_VALUE | Minimum possible value of number |
Number.MAX_VALUE | Max possible value of number |
Mathematical Operations
TypeScript supports below list of mathematical operations on number:
Operation | Description |
abs(num: number) | Returns the absolute value of num |
acos(num: number) | Returns the arccosine of num |
asin(num: number) | Returns the arcsine of num |
atan(num: number) | Returns the arctangent of num |
atan(num: number) | Returns the arctangent of num |
atan2(num1: number, num2: number) | Returns the arctangent of num1/num2 |
ceil(num: number) | Rounds num up to the nearest integer |
cos(num: number) | Returns the cosine of num |
exp(num: number) | Returns the exponential function of num (enum) |
floor(num: number) | Rounds num down to the nearest integer |
log(num) | Returns the natural logarithm of num (loge num) |
max(num1, num2, ...) | Returns the number with the maximum value |
min(num1, num2, ...) | Returns the number with the minimum value |
pow(a, b) | Returns ab |
random() | Returns a random number between 0 and 1 |
round(num: number) | Rounds num to the nearest integer |
sin(num: number) | Returns the sine of num |
sqrt(num: number) | Returns the square root of num |
tan(num: number) | Returns the tangent of num |
All Chapters