typescript-tipo-number

The Number Type in TypeScript

  • 3 min

In TypeScript, the number type is used to represent numbers, both integers and floating-point.

Unlike other languages that may have distinct 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
Copied!

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
Copied!

Arithmetic Operations

TypeScript allows performing basic arithmetic operations with the number type: addition, subtraction, multiplication, division, and modulo.

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
Copied!

Precision and Rounding Errors

Since TypeScript uses JavaScript’s number type, 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
Copied!

To handle precision, especially in financial applications, it is common to use libraries like decimal.js or big.js

Number Methods and Properties

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 positive representable numeric value.
  • Number.POSITIVE_INFINITY: Represents positive infinity.
  • Number.NEGATIVE_INFINITY: Represents negative infinity.
  • Number.NaN: Represents a value that is 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
Copied!

Static Methods

  • Number.isFinite(value): Determines whether the value is a finite number.
  • Number.isInteger(value): Determines whether the value is an integer.
  • Number.isNaN(value): Determines whether the value is NaN.
  • Number.isSafeInteger(value): Determines whether 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
Copied!

Type Conversion

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
Copied!

Infinity

The Infinity value occurs when a numeric operation exceeds the maximum representable range.

let infinity: number = 1 / 0;
console.log(infinity);  // Infinity
Copied!