Summary: in this tutorial, you’ll learn about the TypeScript number data types.
All numbers in TypeScript are either floating-point values or big integers. The floating-point numbers have the type number
while the big integers get the type bigint
.
The following shows how to declare a variable that holds a floating-point value:
let price: number;
Code language: JavaScript (javascript)
Alternatively, you can initialize the price
variable to a number:
let price = 9.95;
Code language: JavaScript (javascript)
As in JavaScript, TypeScript supports the number literals for decimal, hexadecimal, binary, and octal literals:
Decimal numbersThe following shows some decimal numbers:
let counter: number = 0;
let x: number = 100,
y: number = 200;
Code language: JavaScript (javascript)
Binary Numbers
The binary number uses a leading zero followed by a lowercase or uppercase letter “B” e.g., 0b
or 0B
:
let bin = 0b100;
let anotherBin: number = 0B010;
Code language: JavaScript (javascript)
Note that the digit after 0b
or 0B
must be 0
or 1
.
An octal number uses a leading zero followed by the letter o
(since ES2015) 0o
. The digits after 0o
are numbers in the range 0
through 7
:
let octal: number = 0o10;
Code language: JavaScript (javascript)
Hexadecimal numbers
Hexadecimal numbers use a leading zero followed by a lowercase or uppercase letter X
(0x
or 0X
). The digits after the 0x
must be in the range (0123456789ABCDEF
). For example:
let hexadecimal: number = 0XA;
Code language: JavaScript (javascript)
JavaScript has the Number
type (with the letter N
in uppercase) that refers to the non-primitive boxed object. You should not use this Number
type as much as possible in TypeScript.
The big integers represent the whole numbers larger than 253 – 1. A Big integer literal has the n
character at the end of an integer literal like this:
let big: bigint = 9007199254740991n;
Code language: JavaScript (javascript)
Summary
bigint
type.Number
type as much as possible.Was this tutorial helpful ?
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4