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:

PropertyDescription
MAX_VALUEIt specify the largest possible value.
MIN_VALUEIt specify the smallest possible value.
NaNEqual to a value that is not a number.
NEGATIVE_INFINITYA value that is less than MIN_VALUE.
POSITIVE_INFINITYA value that is greater than MAX_VALUE

TypeScript number methods:

MethodsDescription
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);


Please follow and like us:
error








Copyright © 2019 CodesJava DMCA.com Protection Status