A Comprehensive Overview of the number Type in TypeScript
Gain a thorough understanding of the number type in TypeScript. Learn about its floating-point representation, literals, type annotations, arithmetic and comparison operators, and available methods for handling numeric values.
TypeScript Number Data Type
Understanding the number Type
In TypeScript, all numeric values, whether integers, decimals, or large numbers, are represented by the number
type. This aligns with JavaScript's numeric handling.
Key Points:
- Floating-point representation: Numbers are stored as double-precision 64-bit floating-point numbers, following the IEEE 754 standard.
- Number literals: You can use decimal, hexadecimal, octal, and binary literals.
- Type annotations: Explicitly declare variables as
number
for type safety. - Arithmetic operations: Standard arithmetic operators (
+
,-
,*
,/
,%
) are supported. - Comparison operators: Relational operators (
<
,>
,<=
,>=
,==
,!=
) can be used. - Number methods: Various methods are available for formatting, rounding, and converting numbers to strings.
TypeScript Code: Number Examples
let decimalNumber: number = 123.45;
let hexadecimalNumber: number = 0xAF; // Equivalent to 175
let octalNumber: number = 0o77; // Equivalent to 63
let binaryNumber: number = 0b1111; // Equivalent to 15
console.log(decimalNumber);
console.log(hexadecimalNumber);
console.log(octalNumber);
console.log(binaryNumber);
Number Methods
TypeScript provides several built-in methods for working with numbers:
Method | Description |
---|---|
toExponential() |
Returns the number in exponential notation. |
toFixed() |
Returns a string representing the number with a specified number of decimal places. |
toLocaleString() |
Returns a string representation of the number based on the locale. |
toPrecision() |
Returns a string representing the number with a specified precision. |
toString() |
Returns a string representation of the number in a specified base. |
valueOf() |
Returns the primitive value of the number object. |
TypeScript Code: Number Methods
let num = 1234.5678;
console.log(num.toExponential(2)); // Output: 1.23e+3
console.log(num.toFixed(2)); // Output: 1234.57
console.log(num.toLocaleString('en-US')); // Output: 1,234.57
console.log(num.toPrecision(4)); // Output: 1235
console.log(num.toString(2)); // Output: 10011010010.1001100110011001101
console.log(num.valueOf()); // Output: 1234.5678
Number Properties
TypeScript provides several properties on the Number object:
Property | Description |
---|---|
MAX_SAFE_INTEGER |
The maximum safe integer value. |
MIN_SAFE_INTEGER |
The minimum safe integer value. |
MAX_VALUE |
The largest representable number. |
MIN_VALUE |
The smallest positive non-zero number. |
NaN |
Represents "Not a Number". |
NEGATIVE_INFINITY |
Represents negative infinity. |
POSITIVE_INFINITY |
Represents positive infinity. |
Important Considerations
- Precision: Due to the floating-point representation, calculations might involve rounding errors. Use
toFixed()
for precise decimal formatting when needed. - Large numbers: For extremely large numbers, consider using
BigInt
if available in your TypeScript version. - Type safety: TypeScript's type system helps prevent unintended type conversions and errors.
By understanding the number
data type and its methods, you can effectively work with numeric values in your TypeScript applications.