In TypeScript, the type number
is used to represent numbers, both integers and floating-point.
Unlike other languages that may have different types for integers and floating-point numbers, TypeScript (and JavaScript) uses a single number
type for both.
let integer: number = 42;
let decimal: number = 3.14;
let hex: number = 0xff; // Hexadecimal notation
let bin: number = 0b1010; // Binary notation
let oct: number = 0o744; // Octal notation
TypeScript also supports scientific notation for representing very large or very small numbers.
let large: number = 1e6; // 1 * 10^6
let small: number = 1e-6; // 1 * 10^-6
Arithmetic Operations
TypeScript allows performing basic arithmetic operations with the number
type: addition, subtraction, multiplication, division, and modulus.
let a: number = 10;
let b: number = 3;
console.log(a + b); // 13
console.log(a - b); // 7
console.log(a * b); // 30
console.log(a / b); // 3.3333333333333335
console.log(a % b); // 1
Precision and Rounding Errors
Since TypeScript uses the number
type from JavaScript, which is based on the IEEE 754 standard for floating-point numbers, there can be precision issues in operations with decimal numbers.
let x: number = 0.1 + 0.2;
console.log(x); // 0.30000000000000004
To handle precision, especially in financial applications, it is common to use libraries like decimal.js
or big.js
Methods and Properties of Number
TypeScript provides several static methods and properties on the Number
object to help us work with numbers.
Static Properties
Number.MAX_VALUE
: The maximum representable numeric value.Number.MIN_VALUE
: The smallest representable positive numeric value.Number.POSITIVE_INFINITY
: Represents positive infinity.Number.NEGATIVE_INFINITY
: Represents negative infinity.Number.NaN
: Represents a value that is not a number (Not-A-Number).
console.log(Number.MAX_VALUE); // 1.7976931348623157e+308
console.log(Number.MIN_VALUE); // 5e-324
console.log(Number.POSITIVE_INFINITY); // Infinity
console.log(Number.NEGATIVE_INFINITY); // -Infinity
console.log(Number.NaN); // NaN
Static Methods
Number.isFinite(value)
: Determines if the value is a finite number.Number.isInteger(value)
: Determines if the value is an integer.Number.isNaN(value)
: Determines if the value is NaN.Number.isSafeInteger(value)
: Determines if the value is a safe integer (between -(2^53 - 1) and 2^53 - 1).
console.log(Number.isFinite(123)); // true
console.log(Number.isInteger(123.456)); // false
console.log(Number.isNaN(NaN)); // true
console.log(Number.isSafeInteger(Math.pow(2, 53))); // false
Type Conversion
Converting String to Number
It is common to need to convert strings to numbers. TypeScript provides several ways to do this, including parseInt
, parseFloat
, and the unary +
operator.
let string: string = "42";
let integer: number = parseInt(string); // 42
let float: number = parseFloat("3.14"); // 3.14
let number: number = +string; // 42
Converting Number to String
To convert a number to a string, you can use the toString
method.
let num: number = 42;
let str: string = num.toString(); // "42"
Handling Special Numbers
NaN (Not-a-Number)
NaN
is a special value that represents a non-numeric value. It is the result of undefined or illegal operations, such as dividing 0 by 0.
let nan: number = 0 / 0;
console.log(nan); // NaN
console.log(isNaN(nan)); // true
Infinity
The value Infinity
occurs when a numeric operation exceeds the maximum representable range.
let infinity: number = 1 / 0;
console.log(infinity); // Infinity