TypeScript - Numbers
TypeScript - Numbers
Section titled “TypeScript - Numbers”The number type in TypeScript represents numeric values. Unlike some other languages that have distinct types for integers (like int) and floating-point numbers (like float or double), TypeScript’s number type encompasses both.
Internally, it corresponds to JavaScript’s standard double-precision 64-bit floating-point format (IEEE 754).
Declaring Numbers
Section titled “Declaring Numbers”let integerValue: number = 42;let floatValue: number = 3.14159;let hexValue: number = 0xf00d; // Hexadecimallet binaryValue: number = 0b1010; // Binarylet octalValue: number = 0o744; // Octallet bigIntValue: bigint = 9007199254740991n; // Note: bigint is a separate type
console.log(integerValue); // 42console.log(floatValue); // 3.14159console.log(hexValue); // 61453console.log(binaryValue); // 10console.log(octalValue); // 484console.log(bigIntValue); // 9007199254740991nThe Number Object and its Properties
Section titled “The Number Object and its Properties”While you typically work with primitive number values, JavaScript (and thus TypeScript) also has a built-in Number object constructor. This object provides useful static properties related to numbers:
| Property | Description |
|---|---|
Number.MAX_VALUE | The largest positive representable number. |
Number.MIN_VALUE | The smallest positive representable number (closest to zero). |
Number.NaN | Represents “Not-a-Number” - result of invalid math operations (e.g., 0 / 0). |
Number.NEGATIVE_INFINITY | Special value representing negative infinity. |
Number.POSITIVE_INFINITY | Special value representing positive infinity. |
Number.EPSILON | (ES6) Difference between 1 and the smallest floating point number greater than 1. |
Number.MAX_SAFE_INTEGER | (ES6) Maximum integer safely representable without precision loss (2^53 - 1). |
Number.MIN_SAFE_INTEGER | (ES6) Minimum integer safely representable without precision loss (-(2^53 - 1)). |
Example: Number Properties
Section titled “Example: Number Properties”console.log("TypeScript Number Properties:");console.log(`Max Value: ${Number.MAX_VALUE}`);console.log(`Min Value: ${Number.MIN_VALUE}`);console.log(`Negative Infinity: ${Number.NEGATIVE_INFINITY}`);console.log(`Positive Infinity: ${Number.POSITIVE_INFINITY}`);console.log(`NaN: ${Number.NaN}`);console.log(`Max Safe Integer: ${Number.MAX_SAFE_INTEGER}`);Working with NaN
Section titled “Working with NaN”NaN is a special numeric value indicating an unrepresentable result. A key characteristic is that NaN is not equal to anything, including itself (NaN === NaN is false).
To check if a value is NaN, use the global isNaN() function or the more robust Number.isNaN() method (preferred as it doesn’t coerce non-numbers).
let result1: number = Math.sqrt(-1); // Results in NaNlet result2: number = parseInt("not a number"); // Results in NaNlet result3: number = 0 / 0; // Results in NaN
console.log(`Result 1: ${result1}`); // NaN
console.log(`result1 === NaN: ${result1 === Number.NaN}`); // false!
console.log(`isNaN(result1): ${isNaN(result1)}`); // trueconsole.log(`Number.isNaN(result1): ${Number.isNaN(result1)}`); // true (preferred)
console.log(`Number.isNaN("hello"): ${Number.isNaN("hello")}`); // falseconsole.log(`isNaN("hello"): ${isNaN("hello")}`); // true (isNaN coerces "hello" to NaN)Common Number Methods
Section titled “Common Number Methods”Number values (primitives) have access to methods from the Number.prototype.
| Method | Description | Example |
|---|---|---|
toExponential(fractionDigits?) | Returns a string representing the number in exponential notation. | (12345).toExponential(2) is '1.23e+4' |
toFixed(digits?) | Returns a string representing the number in fixed-point notation (with a specific number of decimal places). | (3.14159).toFixed(2) is '3.14' |
toLocaleString(locales?, options?) | Returns a language-sensitive string representation of the number. | (123456.789).toLocaleString('de-DE') might be '123.456,789' |
toPrecision(precision?) | Returns a string representing the number to a specified total number of significant digits. | (123.456).toPrecision(4) is '123.5' |
toString(radix?) | Returns a string representation of the number in the specified base (radix). Default is base 10. | (255).toString(16) is 'ff', (10).toString(2) is '1010' |
valueOf() | Returns the primitive value of the number object (less commonly used directly). | let n = new Number(10); n.valueOf() is 10 |
Example: Number Methods
Section titled “Example: Number Methods”let pi: number = 3.1415926535;let largeNum: number = 1_234_567.89; // Underscores for readability
console.log(`pi.toFixed(3): ${pi.toFixed(3)}`); // 3.142console.log(`largeNum.toExponential(1): ${largeNum.toExponential(1)}`); // 1.2e+6console.log(`largeNum.toLocaleString('en-US'): ${largeNum.toLocaleString('en-US')}`); // 1,234,567.89console.log(`(15).toString(16): ${(15).toString(16)}`); // fconsole.log(`(15).toString(2): ${(15).toString(2)}`); // 1111Number vs. number
Section titled “Number vs. number”Always prefer using the primitive type number for annotations (let x: number). Avoid using the object type Number (let x: Number) unless you have a specific reason, as it refers to the object wrapper type, not the primitive.