Before reading this article, please go through the following articles:
-
toExponential() number object method in typescript
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
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
toFixed(rate:number,subtotal:number)
{
var tax = subtotal * rate; // tax is 11.387500000000001
tax = parseFloat(tax.toFixed(3)); // tax is 11.388
var span = document.createElement("span");
span.innerText = "toFixed Method \n The amount of tax is -> " + tax + "\n";
document.body.appendChild(span);
}
Complete Program
toFixed.ts
class ToFixed_Method
{
toFixed(rate:number,subtotal:number)
{
var tax = subtotal * rate; // tax is 11.387500000000001
tax = parseFloat(tax.toFixed(3)); // tax is 11.388
var span = document.createElement("span");
span.innerText = "toFixed Method \n The amount of tax is -> " + tax + "\n";
document.body.appendChild(span);
}
}
window.onload = () =>
{
var obj = new ToFixed_Method();
var subtotal = parseFloat(prompt("Enter a subtotal"));
var rate = parseFloat(prompt("Enter a rate"));
obj.toFixed(rate,subtotal);
};
toFixed_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="toFixed.js"></script>
</head>
<body>
<h3 style="color:chocolate">toFixed() Number Object Method In TypeScript</h3>
<div id="content"/>
</body>
</html>
toFixed.js
var ToFixed_Method = (function () {
function ToFixed_Method() { }
ToFixed_Method.prototype.toFixed = function (rate, subtotal) {
var tax = subtotal * rate;
tax = parseFloat(tax.toFixed(3));
var span = document.createElement("span");
span.innerText = "toFixed Method \n The amount of tax is -> " + tax + "\n";
document.body.appendChild(span);
};
return ToFixed_Method;
})();
window.onload = function () {
var obj = new ToFixed_Method();
var subtotal = parseFloat(prompt("Enter a subtotal"));
var rate = parseFloat(prompt("Enter a rate"));
obj.toFixed(rate, subtotal);
};
//@ sourceMappingURL=toFixed.js.map
Output 1
Enter a subtotal and click on "Ok".
Output 2
Enter a rate, then click on the "Ok" button and calculate the amount of tax (rounded to three decimal places).
Output 3