In TypeScript, number
is a data type to represent data like 1, 1.5, 0b111001, etc.
number
is stored as a Number
object, where Number
is a wrapper class to manipulate numbers as objects.int
, float
, or double
. Everything is simply a number
in TypeScript.var variable_name:number = value
The following example shows the usage of data type number
. Here, y
is the variable name, number
is the data type, and 5
is the value.
var y:number = 5console.log(y)
The following code snippet shows that we can assign float, binary, and hexadecimal values to the number
datatype.
// representing floating point numbersvar f:number = 1.2console.log(f)//representing decimal numbersvar b:number = 0b111001console.log(b)//represeting hexadecimal numbersvar h:number = 0x37CFconsole.log(h)
We can perform many math operations on numbers
, like addition, subtraction, multiplication, etc.
The following code snippet shows the usage of some of the operators.
var x:number = 10var y:number = 20//additionconsole.log(`Addition = ${x+y}`)//subtractionconsole.log(`Subtraction = ${y-x}`)//Multiplicationconsole.log(`Multiplication = ${x*y}`)
The Number
wrapper provides the following methods on numbers:
toExponential()
toFixed()
toLocaleString()
toPrecision()
toString()
valueOf()
We will go through each of them in detail.
toExponential()
This method returns the exponential notation of a given number in string format. It accepts one optional parameter to specify the number of digits to be placed after the decimal point.
var x:number = 1357//without any parameterconsole.log(x.toExponential())//with 1 as parameter, one digit will present after decimal pointconsole.log(x.toExponential(1))
toFixed()
This method returns a fixed-point notation of the given number in string format. It accepts one optional parameter to specify the number of digits after the decimal point.
var x:number = 20.8938//without paramters, it will be fixed to 21console.log(x.toFixed())//2 as parameter, so 2 digits will present after decimalconsole.log(x.toFixed(2))
toLocaleString()
This method will return the given number in local string format. Please try to execute the following code snippet in TypeScript playground.
let x: number = 11677.123;//returns "11,677.123" in US English formatconsole.log(x.toLocaleString());// returns "11.677,123" in Germanconsole.log(x.toLocaleString('de-DE'));// returns "١١٬٦٧٧٫١٢٣" in Arabic formatconsole.log(x.toLocaleString('ar-EG'));
toPrecision()
This method returns a fixed-point or exponential format for the given number. It accepts an optional parameter to specify the number of digits for precision.
var x:number = 11.5987// 1 as paramter to specify precision to 1 digitconsole.log(x.toPrecision(1))// 2 as paramter to specify precision to 2 digitsconsole.log(x.toPrecision(2))
toString()
This method returns the number in string format for a given base.
The base must be between and .
var x:number = 987//converting 987 into base 2 formatconsole.log(x.toString(2))
valueOf()
This method returns the primitive type of the given object. We can create a number
object with the Number
class.
//constructing number objectvar x = new Number(20)//printing primitive value of object xconsole.log(x.valueOf())