Number Object Method In TypeScript: Part 2

Number Object Method In TypeScript: Part 2 

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

Introduction

 
In TypeScript, the number object is a wrapper for the 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 the numerical values.
In this article, I am describing the toFixed method of the number object method in TypeScript.
 

toFixed() Method

 
In TypeScript the toFixed() method is used to return a string with the number rounded to the specified number of decimal places. Zeroes are added to create the desired decimal length if the desired number of digits is greater than the actual number.
 
Syntax
  1. array.toFixed(digit)  
digit Is an optional parameter. The number of digits to appear after the decimal point. The default value of "digit" in the toFixed method is 0; in other words, no digit after the decimal point.
 
The following example shows how to use the toFixed method in TypeScript. In this example, we get a subtotal and a rate from the user. Then it calculates the amount of tax.
 
For the values, show, this number is 11.387500000000001 and must be rounded to three decimal places.
 
Function
  1. toFixed(rate: number, subtotal: number) {  
  2.  var tax = subtotal * rate;  
  3.  tax = parseFloat(tax.toFixed(3));  
  4.  var span = document.createElement("span");  
  5.  span.innerText = "toFixed Method \n The amount of tax is -> " + tax + "\n";  
  6.  document.body.appendChild(span);  
  7. }  

Complete Program

 
toFixed.ts
  1. class ToFixed_Method {  
  2.  toFixed(rate: number, subtotal: number) {  
  3.   var tax = subtotal * rate;  
  4.   tax = parseFloat(tax.toFixed(3));  
  5.   var span = document.createElement("span");  
  6.   span.innerText = "toFixed Method \n The amount of tax is -> " + tax + "\n";  
  7.   document.body.appendChild(span);  
  8.  }  
  9. }  
  10. window.onload = () => {  
  11.  var obj = new ToFixed_Method();  
  12.  var subtotal = parseFloat(prompt("Enter a subtotal"));  
  13.  var rate = parseFloat(prompt("Enter a rate"));  
  14.  obj.toFixed(rate, subtotal);  
  15. };  
toFixed_MethodDemo.htm
  1. < !DOCTYPE html >  
  2.  <  
  3.  html lang = "en"  
  4. xmlns = "http://www.w3.org/1999/xhtml" >  
  5.  <  
  6.  head >  
  7.  <  
  8.  meta charset = "utf-8" / >  
  9.  <  
  10.  title > TypeScript HTML App < /title>  
  11.  <  
  12.  link rel = "stylesheet"  
  13. href = "app.css"  
  14. type = "text/css" / >  
  15.  <  
  16.  script src = "toFixed.js" > < /script>  
  17.  <  
  18.  /head>  
  19.  <  
  20.  body >  
  21.  <  
  22.  h3 style = "color:chocolate" > toFixed() Number Object Method In TypeScript < /h3>  
  23.  <  
  24.  div id = "content" / >  
  25.  <  
  26.  /body>  
  27.  <  
  28.  /html>  
toFixed.js
  1. var ToFixed_Method = (function() {  
  2.  function ToFixed_Method() {}  
  3.  ToFixed_Method.prototype.toFixed = function(rate, subtotal) {  
  4.   var tax = subtotal * rate;  
  5.   tax = parseFloat(tax.toFixed(3));  
  6.   var span = document.createElement("span");  
  7.   span.innerText = "toFixed Method \n The amount of tax is -> " + tax + "\n";  
  8.   document.body.appendChild(span);  
  9.  };  
  10.  return ToFixed_Method;  
  11. })();  
  12. window.onload = function() {  
  13.  var obj = new ToFixed_Method();  
  14.  var subtotal = parseFloat(prompt("Enter a subtotal"));  
  15.  var rate = parseFloat(prompt("Enter a rate"));  
  16.  obj.toFixed(rate, subtotal);  
  17. };  
Output 1
Enter a subtotal and click on "Ok".
 
 enter-subtotal.gif
 
Output 2
Enter a rate, then click on the "Ok" button and calculate the amount of tax (rounded to three decimal places).
 
enter-rate.gif
 
Output 3
 
result.gif


Similar Articles