Number Object Method In TypeScript: Part 1

Introduction

 
In TypeScript, the number object is an object wrapper for primitive numeric values. If the value parameter cannot be converted to a number then it will return NaN. There are four methods of the number object that allow you to format the display of numerical values.
 
In this article, I am describing the toExponential method of the number object method in the TypeScript.
 

toExponential() Method

 
In TypeScript, the toExponential() method returns a numeric string with the number in exponential format with the specified number of digits after the decimal place. If a digit is not provided then it will return all digits. This method is useful for displaying scientific values but if you are not developing scientific applications then you probably won't need this method.
 
Syntax
  1. array.toExponential(number)   
number is an optional parameter. An integer between 0 and 20 specifying the number of digits in the notation after the decimal point. Many digits as necessary to represent the value.
 
The following example shows how to use the toExponential() method in TypeScript. We get a mass from the user in kilograms and then use Einstein's mass-energy equation to convert the kilograms to Joules and display the result in exponential format to 4 digits. Because a small amount of mass stores a large amount of energy, the exponential format is more convenient in this example.
 
Function
  1. toExponential(Mass: number) {  
  2.  var c = 2.99792458  
  3.  var E = Mass * c * c;  
  4.  var span = document.createElement("span");  
  5.  span.innerText = "toExponential Method \n The amount of energy is -> " + E.toExponential(4) + "\n";  
  6.  document.body.appendChild(span);  
  7. }  

Complete Program

 
toExponential.ts
  1. class ToExponential_Method {  
  2.  toExponential(Mass: number) {  
  3.   var c = 2.99792458  
  4.   var E = Mass * c * c;  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "toExponential Method \n The amount of energy is -> " + E.toExponential(4) + "\n";  
  7.   document.body.appendChild(span);  
  8.  }  
  9. }  
  10. window.onload = () => {  
  11.  var obj = new ToExponential_Method();  
  12.  var Mass = parseFloat(prompt("Enter a mass in Kg."));  
  13.  obj.toExponential(Mass);  
  14. };  
toExponential_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="toExponential.js"></script>  
  9.     </head>  
  10.     <body>  
  11.         <h3 style="color:blueviolet">toExponential() Number Object Method In TypeScript</h3>  
  12.         <div id="content"/>  
  13.     </body>  
  14. </html>  
toExponential.js
  1. var ToExponential_Method = (function() {  
  2.  function ToExponential_Method() {}  
  3.  ToExponential_Method.prototype.toExponential = function(Mass) {  
  4.   var c = 2.99792458;  
  5.   var E = Mass * c * c;  
  6.   var span = document.createElement("span");  
  7.   span.innerText = "toExponential Method \n The amount of energy is -> " + E.toExponential(4) + "\n";  
  8.   document.body.appendChild(span);  
  9.  };  
  10.  return ToExponential_Method;  
  11. })();  
  12. window.onload = function() {  
  13.  var obj = new ToExponential_Method();  
  14.  var Mass = parseFloat(prompt("Enter a mass in Kg."));  
  15.  obj.toExponential(Mass);  
  16. };  
Output 1
 
Enter a mass in kg.
 
enter-mass.gif
 
Output 2
 
result.gif


Similar Articles