CodesJava

Easy learning with example program codes

TypeScript number object tutorial


TypeScript number object:

The TypeScript Number object represents numerical data which can be integers or floating-point numbers.

TypeScript number constants/properties:

Property Description
MAX_VALUE It specify the largest possible value.
MIN_VALUE It specify the smallest possible value.
NaN Equal to a value that is not a number.
NEGATIVE_INFINITY A value that is less than MIN_VALUE.
POSITIVE_INFINITY A value that is greater than MAX_VALUE

TypeScript number methods:

Methods Description
toExponential(fractionDigits) It returns a string representing theĀ number object in exponential notation.
toFixed(digits) It limits the number of digits after decimal value.
toPrecision(digits) It formats the number with given number of digits.
toString() It converts number into string.
valueOf() It converts other type of value into number.

How to create TypeScript number object:

  • By using number literals.
  • By using Number() constructor.

By using number literals:

When we create a number literal browser automatically converts it to Number object.

Syntax:

var num1:number = value;

Example:

function numTest(num1:number):void{
  console.log(num1); 
}
numTest(123);

Try it:

JS Bin on jsbin.com

By using Number() constructor:

We can create a TypeScript Number object by using Number() constructor. It returns NaN if value is not a number.

Syntax:

var num1 = new Number(value);

Example:

function numTest(num1:number):void{
  console.log(num1); 
}
var num1 = new Number(123);
var num2 = new Number("Hello");
numTest(num1);
numTest(num2);

Try it:

JS Bin on jsbin.com

Please follow and like us:


Copyright © 2019 CodesJava DMCA.com Protection Status SiteMap Reference: Java Wiki