Number Object Methods in TypeScript

The Number object have only the default methods that are present in any other object. Properties of Number object are explained in another article.
Below are some of the commonly used methods:
valueOf()
toString()
toPrecision()
toLocaleString()
toFixed()
toExponential()

toExponential()

This methods returns the exponential notation of the Number object in the string data type.
Syntax:

numberVariable.toExponential(numberOfDecimals?:number)

The parameter in this method is a number which specifies the number of decimal digits after decimal point. It is optional.
Below is an example:

var num1=123.456789;
var result=num1.toExponential();
console.log(result);
var result1=num1.toExponential(2);
console.log(result1);

Below is the output of above code:

toFixed()

This function is used to fix the the decimal digit in a fractional number. We pass the number of digits we want after decimal point as the parameter.
Syntax:

numberVariable.toFixed(numberOfDecimals?:number)

Below is an example:

var num1=123.4567;
console.log(num1.toFixed());
console.log(num1.toFixed(1));
console.log(num1.toFixed(5));

Below is the output of above code:

toLocaleString()

This method converts a number object into a human readable string representing the number using the locale of the environment.
Syntax:

numberVariable.toLocaleString();

Below is an example:

var num1=123.456;
var str=num1.toLocaleString();
console.log(num1.toLocaleString());
console.log("typeof str: " + typeof(str));

Below is the output of above code

toPrecision()

This method returns a string representing the number of digits to the mentioned precision. Parameter takes the number of digits for the precision and is optional.
Syntax

numberVariable.toPrecision(precisionNumber?:number)

Below is an example:

var num=12.3456;
console.log(num.toPrecision());
console.log(num.toPrecision(2));
console.log(num.toPrecision(4));
var str=num.toPrecision(2);
console.log(typeof(str));

Below is the output of above code

toString()

This method will convert a object into a string type. We can then use String methods on this object.
Syntax:

variable.toString()

Below is an example

var num=1234;
console.log(num.toString());
console.log(typeof(num.toString()));

Below is the output of above code:

valueOf()

Returns the primitive value of an object. This can be any object, number, string etc.
Syntax:

variable.valueOf();

Below is an example:

var num = new Number(100); 
console.log(num.valueOf());
var str = new String("Hello World!!"); 
console.log(str.valueOf());

Below is the output of above code:

Properties of Number object in TypeScript

We use Number class to perform actions on numbers or access their properties. Methods for Number object are explained in another article.
Syntax:

var variableName = new Number(value)

In case as non-numeric value is passed to the constructor, it will return NaN(Not-a-Number).
Below is a list of properties of of Number class:

Property Name Description Example
MAX_VALUE This represents the maximum value a Numeric variable can hold Number.MAX_VALUE
MIN_VALUE This represents the minimum value a Numeric variable can hold Number.MIN_VALUE
NaN This represents a value that is not a number Number.NaN
NEGATIVE_INFINITY This represents the value that is less than Minimum value Number.NEGATIVE_INFINITY
POSITIVE_INFINITY This represents a value that is more than Maximum value Number.POSITIVE_INFINITY

Below is a code to get the above values.

var num=new Number();
console.log("Maximum value for a Numerical variable: " + Number.MAX_VALUE);
console.log("Minimum value for a Numerical variable: " + Number.MIN_VALUE);
console.log("Value that is more than Maximum value: " + Number.POSITIVE_INFINITY);
console.log("Value that is less than Minimum value: " + Number.NEGATIVE_INFINITY);
console.log("Not-a-Number Value: " + Number.NaN);

Below is the output of above code:

Power Platform Academy

Start or Upgrade your Career with Power Platform

Learn with Akanksha

Python | Azure | AI/ML | OpenAI | MLOps

Design a site like this with WordPress.com
Get started