TypeScript Number Object
In TypeScript, number is a primitive data type used to represent numbers, including integers and floating-point numbers. In addition to this primitive type, there is a corresponding built-in object Number. The Number object is a wrapper object for the primitive number type, providing some properties and methods for working with numbers.
Typically, using the number type directly is sufficient. When you call a method on a number type variable (like toFixed()), JavaScript/TypeScript automatically wraps it in a temporary Number object behind the scenes to perform the operation.
Properties of the Number Object
The Number object provides some useful built-in properties that are static and can be accessed directly through Number.
Number.MAX_VALUE: Represents the largest positive number that can be represented in JavaScript.typescriptconsole.log(Number.MAX_VALUE); // Approximately 1.797e+308Number.MIN_VALUE: Represents the smallest positive number that can be represented in JavaScript (the positive number closest to 0).typescriptconsole.log(Number.MIN_VALUE); // Approximately 5e-324Number.NaN: Represents the special "Not-a-Number" value. When an arithmetic operation returns an undefined or unrepresentable value, you getNaN.typescriptconsole.log(0 / 0); // NaN console.log(parseInt("hello")); // NaNNumber.POSITIVE_INFINITY: Represents positive infinity.typescriptconsole.log(Number.POSITIVE_INFINITY); // Infinity console.log(1 / 0); // InfinityNumber.NEGATIVE_INFINITY: Represents negative infinity.typescriptconsole.log(Number.NEGATIVE_INFINITY); // -Infinity console.log(-1 / 0); // -Infinity
Methods of the Number Object
These methods need to be called through an instance of the number type.
toFixed(digits): Formats a number to a string with a specified number of decimal places. It performs rounding.typescriptlet num = 123.45678; console.log(num.toFixed(2)); // "123.46" console.log(num.toFixed(0)); // "123"toPrecision(precision): Formats a number to a string with a specified total length (significant digits). It also performs rounding.typescriptlet num = 123.45678; console.log(num.toPrecision(4)); // "123.5" console.log(num.toPrecision(7)); // "123.4568" console.log(num.toPrecision(2)); // "1.2e+2"toString(radix): Converts a number to a string representation in the specified base (radix).radixis an integer between 2 and 36. If omitted, defaults to 10.typescriptlet num = 255; console.log(num.toString()); // "255" (decimal) console.log(num.toString(16)); // "ff" (hexadecimal) console.log(num.toString(8)); // "377" (octal) console.log(num.toString(2)); // "11111111" (binary)toExponential(fractionDigits): Returns a string representing the number in exponential notation.fractionDigitsis optional and specifies the number of digits in the fractional part.typescriptlet num = 12345; console.log(num.toExponential()); // "1.2345e+4" console.log(num.toExponential(2)); // "1.23e+4"toLocaleString(): Returns a localized string representation of the number. This is useful when displaying currency, dates, etc., as it formats numbers according to the user's locale settings.typescriptlet largeNum = 1234567.89; // In US English locale console.log(largeNum.toLocaleString('en-US')); // "1,234,567.89" // In German locale console.log(largeNum.toLocaleString('de-DE')); // "1.234.567,89"
These methods provide powerful flexibility for handling and displaying numbers.