Number Object Of TypeScript

The number is also supported in typescript as, like javascript, the number object converts into a numeric latter to an instant of the class Number. The Number is a class that makes numeric literals an object. To declare a number first we have to write var or let keyword to make the typescript that the next one is a variable name, then followed by equal sign have to make an object of number class by providing new keyword and then the class name Number and after a number have to send the number value surrounded by parenthesis or first bracket.
The syntax is like the format given below.
var variable_name = new Number(value);

Example:

var variable_name = new Number(value);

The properties of the Number object

There are a few properties of number objects which are given below.

Number.MAX_VALUE: it is used to get the largest possible values a javascript can hold.

Example:
console.log("Maximum value that a number variable can hold: " + Number.MAX_VALUE); 

Number.MIN_VALUE: is used to get the smallest possible value a number can hold

Example:
console.log("Minimum value that a number variable can hold: " + Number.MIN_VALUE); 

Number.NEGATIVE_INFINITY: is a value that is less than the minimum value

Example:
console.log("Value of Negative Infinity which is less than minimum number: " + Number.NEGATIVE_INFINITY); 

Number.POSITIVE_INFINITY: is a value that is greater than the maximum value

Example:
console.log("Value of Positive Infinity which is grater than the maximum number:" + Number.POSITIVE_INFINITY);

NaN: makes a value that is not a number

Example:
var years = 12;

if(years<=10 || years>=12){
    years = Number.NaN;
    console.log('Current Years is : ' + years);
}else{
    console.log('The value is not accepted in the if block');
}

Prototype: A prototype is used to assign new property or method to the current number object

An example of the prototype is given below

function employee_details(id:number,name:string){
    this.id = id;
    this.name = name;
}

var emp = new employee_details(1,'Hiranmoy Mondal');
employee_details.prototype.email = '[email protected]';

console.log(emp.id);
console.log(emp.name);
console.log(emp.email);

Constructor: returns the object’s instance which is created by the function and by default its an object of Number

The Methods of number object:

The number object supports some common methods which are part of every defined object. The method is described below.

toExponential()

A string representing a Number object in exponential notation with one digit before the decimal point rounded to fraction digits after the decimal point. If the fraction digits argument is omitted, the number of digits after the decimal point defaults to the number of digits necessary to represent the value uniquely.

Example:
var number_val = 25.30 
var exponencial_val = number_val.toExponential(); 
console.log(exponencial_val);

toFixed() made a number with specific digit after decimal points style: number.toFixed( [digits] )

Example:
var num_val = 177.234 
console.log("num_val.toFixed() is "+num_val.toFixed()) 
console.log("num_val.toFixed(2) is "+num_val.toFixed(2)) 
console.log("num_val.toFixed(6) is "+num_val.toFixed(5))

number.toLocaleString(): it converts a number into a human-readable string that can be read easily

Example:
var num = new Number(177.1234); 
console.log( num.toLocaleString());

number.toPrecision( [ precision ] ): This method returns a string representing the Number object to the specified precision.

Example:
var num = new Number(7.123456); 
console.log(num.toPrecision()); 
console.log(num.toPrecision(1)); 
console.log(num.toPrecision(2));

number.toString( [radix] ) : use to convert the number to string with specific base

Example:
var num = new Number(10); 
console.log(num.toString()); 
console.log(num.toString(2)); 
console.log(num.toString(8));

number.valueOf() : This method returns the primitive value of the specified number object.

Example:
var num = new Number(10); 
console.log(num.valueOf());

Share The Post On -