TypeScript Data Type - Number

Just like JavaScript, TypeScript supports number data type. All numbers are stored as floating point numbers. These numbers can be Decimal (base 10), Hexadecimal (base 16) or Octal (base 8).

Example: TypeScript Number Type Variables
let first:number = 123; // number 
let second: number = 0x37CF;  // hexadecimal
let third:number=0o377 ;      // octal
let fourth: number = 0b111001;// binary  

console.log(first);  // 123 
console.log(second); // 14287
console.log(third);  // 255
console.log(fourth); // 57 

In the above example, let first:number = 1; stores a positive integer as a number. let second: number = 0x37CF; stores a hexadecimal as a number which is equivalent to 14287. When you print this number on your browser's console, it prints the equivalent floating point of the hexadecimal number. let third:number=0377; stores an octal number equivalent to 255.

Number Methods

Method Description
toExponential() Returns the exponential notation in string format.
toFixed() Returns the fixed-point notation in string format.
toLocaleString() Converts the number into a local specific representation of the number.
toPrecision() Returns the string representation in exponential or fixed-point to the specified precision.
toString() Returns the string representation of the number in the specified base.
valueOf() Returns the primitive value of the number.

toExponential()

The toExponential method returns the exponential notation of a number in string format, based on the specified fraction digits.

Signature:
numObj.toExponential([fractionDigits])

This function takes in one argument, an optional integer specifying the number of digits after the decimal point. It returns the exponential representation of the number in a string format.

Example: toExponential()
let myNumber: number = 123456;

myNumber.toExponential(); // returns 1.23456e+5
myNumber.toExponential(1); //returns 1.2e+5
myNumber.toExponential(2); //returns 1.23e+5
myNumber.toExponential(3); //returns 1.235e+5

toFixed()

The toFixed method returns the fixed-point notation of a number in string format.

Signature:
numObj.toFixed([digits])

This function has one optional argument, an integer specifying the number of digits after the decimal point. It returns a string representation of the formatted number.

Example: toFixed()
let myNumber: number = 10.8788;

myNumber.toFixed(); // returns 11
myNumber.toFixed(1); //returns 10.9
myNumber.toFixed(2); //returns 10.88
myNumber.toFixed(3); //returns 10.879
myNumber.toFixed(4); //returns 10.8788

toLocaleString()

The toLocaleString method converts the number into a local specific representation of the number.

Signature:
numObj.toLocaleString([locales [, options]])

This function takes in two arguments: the locale, and an optional options argument representing the locale to which you would like to get the equivalent number representation. In the below example, we pass the string 'de-DE' which is the locale for German.

Example: toLocaleString()
let myNumber: number = 10667.987;

myNumber.toLocaleString(); // returns 10,667.987 - US English
myNumber.toLocaleString('de-DE'); // returns 10.667,987 - German
myNumber.toLocaleString('ar-EG'); // returns 10667.987 in Arebic

toPrecision()

The toPrecision method returns the string representation in exponential or fixed-point to the specified precision.

Signature:
numObj.toPrecision([precision])

This function takes in one optional argument, a number representing the precision i.e. the number of significant digits. It returns a string representation to the specified precision.

Example: toPrecision()
let myNumber: number = 10.5679;

myNumber.toPrecision(1); // returns 1e+1
myNumber.toPrecision(2); // returns 11
myNumber.toPrecision(3); // returns 10.6
myNumber.toPrecision(4); // returns 10.57

toString()

The toString method returns a string representation of the number in the specified base.

Signature:
numObj.toString([radix])

This function takes in one optional argument, a radix number representing the base for which a string representation is to be returned. The radix value should be between 2 and 36.

Example: toString()
let myNumber: number = 123;
myNumber.toString(); // returns '123'
myNumber.toString(2); // returns '1111011'
myNumber.toString(4); // returns '1323'
myNumber.toString(8); // returns '173'
myNumber.toString(16); // returns '7b'
myNumber.toString(36); // returns '3f'

valueOf()

The valueOf method returns the primitive value of the number.

Signature:
numObj.valueOf()

This function returns the primitive value of the Object being called on.

Example: valueOf()
let myNumber = new Number(123);
console.log(myNumber) //Output: a number object with value 123
console.log(myNumber.valueOf()) //Output: 123
console.log(typeof num) //Output: object

let num2 = num.valueOf() 
console.log(num2) //Output: 123
console.log(typeof num2) //Output: number