In TypeScript, the number object is an object wrapper for primitive numeric values. If the value parameter can not be converted into a number, it will be return NaN. There are four methods of the number object that allow you to format the display of the numerical values.

In this article I am describing the toStringmethod of the number object method in TypeScript.

toString() Method

In TypeScript, the toString() method returns a string with the number in the given base. The base can range from 2 to 36. The default base is 10 if the base is not provided.

Syntax

array.toString(base)

base:is an optional parameter. An integer in the range between 2 and 36 specifying the base to use for representing numeric values.

The following example shows how to use the toString method in TypeScript. In this example, we have two methods, simple_toString and Base_toString. In the simple_toString method, the integer value in age is automatically converted to a string by the toString method when the age is concatenated with a string literal. This implicit conversion is done whenever a number is used in an expression that mixes string and number values. As a result, you only need to use the toString method explicitly when you need to convert a number to a base other than 10.

Another method is Base_toString. This method converts a string to binary or hexadecimal values. If you are familiar with binary and hexadecimal values then you may find this useful, but otherwise you should not need to do this.