Number Object Method In TypeScript: Part 4

Number Object Method In TypeScript: Part 4 

 
Before reading this article, please go through the following articles:

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 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
  1. 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.
 
Function
 
The following is a sample use of the toString method for base 10:
  1. simple_toString(age: number) {  
  2.  var span = document.createElement("span");  
  3.  span.innerText = "Implicit use of toString method for base 10 conversions \n Your age in decimal is-> " + age.toString() + "\n";  
  4.  document.body.appendChild(span);  
  5. }  
The following is a sample use of the toString method with another base:
 
Base_tostring(age:number)
  1. {  
  2.  var span = document.createElement("span");  
  3.  span.style.color = "green";  
  4.  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);  
  5.  document.body.appendChild(span);  
  6. }  

Complete Program

 
toString.ts
  1. class ToString_Method  
  2. {  
  3.  simple_toString(age: number)  
  4.  {  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "Implicit use of toString method for base 10 conversions \n Your age in decimal is-> " + age.toString() + "\n";  
  7.   document.body.appendChild(span);  
  8.  }  
  9.  Base_tostring(age: number)  
  10.  {  
  11.   var span = document.createElement("span");  
  12.   span.style.color = "green";  
  13.   span.innerText = "Using of toString method with other bases \n Your age in binary is-> " + age.toString(2) + "\n" +  
  14.    "Your age in hexadecimal is-> " + age.toString(16);  
  15.   document.body.appendChild(span);  
  16.  }  
  17. }  
  18. window.onload = () =>  
  19.  {  
  20.   var obj = new ToString_Method();  
  21.   var age = parseInt(prompt("Please enter your age"));  
  22.   obj.simple_toString(age);  
  23.   obj.Base_tostring(age);  
  24.  };  
toString_MethodDemo.htm
  1. <!DOCTYPE html>  
  2. <html lang="en"  
  3.     xmlns="http://www.w3.org/1999/xhtml">  
  4.     <head>  
  5.         <meta charset="utf-8" />  
  6.         <title>TypeScript HTML App</title>  
  7.         <link rel="stylesheet" href="app.css" type="text/css" />  
  8.         <script src="toString.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3 style="color:chocolate">toString() Number Object Method In TypeScript</h3>  
  12.         <div id="content"/>  
  13.     </body>  
  14. </html>  
toString.js
  1. var ToString_Method = (function() {  
  2.  function ToString_Method() {}  
  3.  ToString_Method.prototype.simple_toString = function(age) {  
  4.   var span = document.createElement("span");  
  5.   span.innerText = "Implicit use of toString method for base 10 conversions \n Your age in decimal is-> " + age.toString() + "\n";  
  6.   document.body.appendChild(span);  
  7.  };  
  8.  ToString_Method.prototype.Base_tostring = function(age) {  
  9.   var span = document.createElement("span");  
  10.   span.style.color = "green";  
  11.   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);  
  12.   document.body.appendChild(span);  
  13.  };  
  14.  return ToString_Method;  
  15. })();  
  16. window.onload = function() {  
  17.  var obj = new ToString_Method();  
  18.  var age = parseInt(prompt("Please enter your age"));  
  19.  obj.simple_toString(age);  
  20.  obj.Base_tostring(age);  
  21. };  
Output 1
 
Enter your age and click on "Ok":
 
 enter-age.gif
 
Output 2
 
result.gif