Math Object in TypeScript: Part 3
Before reading this article, please go through the following articles:
Introduction
In this article, I am describing some of the TypeScript math functions.
Floor() Method
Syntax
- floor (number)
Function
- Floor(num: number) {
- var span = document.createElement("span");
- span.innerText = "The Floor value of " + num + " is -> " + Math.floor(num) + "\n";
- document.body.appendChild(span);
- }
Log() Method
In TypeScript the log() method returns the logarithm of a number. By default the number base is E. If the specified number is negative then Nan is returned and if the number is 0 then -Infinity is returned.
- log (number)
Function
- Log(num: number) {
- var span = document.createElement("span");
- span.innerText = "The Logarithm of " + num + " is -> " + Math.log(num) + "\n";
- document.body.appendChild(span);
- }
Round() Method
For example, 4.45 will be rounded down and 4.5 will be rounded up.
Syntax
- round(number)
Function
- Round(num: number) {
- var span = document.createElement("span");
- span.innerText = "The Round value of " + num + " is -> " + Math.round(num);
- document.body.appendChild(span);
- }
Complete Program
Floor_Log_Round.ts
- class Math_Method
- {
- Floor(num: number)
- {
- var span = document.createElement("span");
- span.innerText = "The Floor value of " + num + " is -> " + Math.floor(num) + "\n";
- document.body.appendChild(span);
- }
- Log(num: number)
- {
- var span = document.createElement("span");
- span.innerText = "The Logarithm of " + num + " is -> " + Math.log(num) + "\n"; // By default base E of number
- document.body.appendChild(span);
- }
- Round(num: number)
- {
- var span = document.createElement("span");
- span.innerText = "The Round value of " + num + " is -> " + Math.round(num);
- document.body.appendChild(span);
- }
- }
- window.onload = () =>
- {
- var obj = new Math_Method();
- var num = parseFloat(prompt("Enter a number for calculate Floor, Log and Round"));
- obj.Floor(num);
- obj.Log(num);
- obj.Round(num);
- };
Floor_Log_Round_Demo.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="Floor_Log_Round.js"></script>
- </head>
- <body>
- <h3>Floor, Log and Round Math Function in TypeScript</h3>
- <div id="content"/>
- </body>
- </html>
Floor_Log_Round.js
- var Math_Method = (function() {
- function Math_Method() {}
- Math_Method.prototype.Floor = function(num) {
- var span = document.createElement("span");
- span.innerText = "The Floor value of " + num + " is -> " + Math.floor(num) + "\n";
- document.body.appendChild(span);
- };
- Math_Method.prototype.Log = function(num) {
- var span = document.createElement("span");
- span.innerText = "The Logarithm of " + num + " is -> " + Math.log(num) + "\n";
- document.body.appendChild(span);
- };
- Math_Method.prototype.Round = function(num) {
- var span = document.createElement("span");
- span.innerText = "The Round value of " + num + " is -> " + Math.round(num);
- document.body.appendChild(span);
- };
- return Math_Method;
- })();
- window.onload = function() {
- var obj = new Math_Method();
- var num = parseFloat(prompt("Enter a number for calculate Floor, Log and Round"));
- obj.Floor(num);
- obj.Log(num);
- obj.Round(num);
- };
Output 1

Output 2


Comments
Join the conversation! Your thoughts help the community grow.