Before reading this article, please go through the following articles:
-
toExponential() number object method in typescript
-
toFixed() number object method in typescript
-
toPrecision() number object method in typescript
Introduction
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 toString method 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
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.
Function
The following is a sample use of the toString method for base 10:
simple_toString(age:number)
{
var span = document.createElement("span");
span.innerText = "Implicit use of toString method for base 10 conversions \n Your age in decimal is-> " + age.toString() + "\n";
document.body.appendChild(span);
}
The following is a sample use of the toString method with another base:
Base_tostring(age:number)
{
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "Using of toString method with other bases \n Your age in binary is-> " + age.toString(2) + "\n"+
"Your age in hexadecimal is-> " + age.toString(16);
document.body.appendChild(span);
}
Complete Program
toString.ts
class ToString_Method
{
simple_toString(age:number)
{
var span = document.createElement("span");
span.innerText = "Implicit use of toString method for base 10 conversions \n Your age in decimal is-> " + age.toString() + "\n";
document.body.appendChild(span);
}
Base_tostring(age:number)
{
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "Using of toString method with other bases \n Your age in binary is-> " + age.toString(2) + "\n"+
"Your age in hexadecimal is-> " + age.toString(16);
document.body.appendChild(span);
}
}
window.onload = () =>
{
var obj = new ToString_Method();
var age = parseInt(prompt("Please enter your age"));
obj.simple_toString(age);
obj.Base_tostring(age);
};
toString_MethodDemo.htm
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>TypeScript HTML App</title>
<link rel="stylesheet" href="app.css" type="text/css" />
<script src="toString.js"></script>
</head>
<body>
<h3 style="color:chocolate">toString() Number Object Method In TypeScript</h3>
<div id="content"/>
</body>
</html>
toString.js
var ToString_Method = (function () {
function ToString_Method() { }
ToString_Method.prototype.simple_toString = function (age) {
var span = document.createElement("span");
span.innerText = "Implicit use of toString method for base 10 conversions \n Your age in decimal is-> " + age.toString() + "\n";
document.body.appendChild(span);
};
ToString_Method.prototype.Base_tostring = function (age) {
var span = document.createElement("span");
span.style.color = "green";
span.innerText = "Using of toString method with other bases \n Your age in binary is-> " + age.toString(2) + "\n" +
"Your age in hexadecimal is-> " + age.toString(16);
document.body.appendChild(span);
};
return ToString_Method;
})();
window.onload = function () {
var obj = new ToString_Method();
var age = parseInt(prompt("Please enter your age"));
obj.simple_toString(age);
obj.Base_tostring(age);
};
//@ sourceMappingURL=toString.js.map
Output 1
Enter your age and click on "Ok":
Output 2