Reader Level:
Articles

Number Object Method In TypeScript: Part 1

By Nitin Bhardwaj on January 18, 2013
In this article, you will learn about the number object method in TypeScript.
  • 0
  • 0
  • 2500

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 be 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

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. A 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

toExponential(Mass:number)

    {

     var c=2.99792458  // Actual value of C in Einstein's mass-energy Formula

     var E = Mass * c * c;// E=mc2, Einstein's mass-energy Formula

     var span = document.createElement("span");

     span.innerText = "toExponential Method \n The amount of energy is -> " + E.toExponential(4) + "\n";

     document.body.appendChild(span);

    }

 

 

Complete Program

toExponential.ts

class ToExponential_Method

{

    toExponential(Mass:number)

    {

     var c=2.99792458  // Actual value of C in Einstein's mass-energy Formula

     var E = Mass * c * c;// E=mc2, Einstein's mass-energy Formula

     var span = document.createElement("span");

     span.innerText = "toExponential Method \n The amount of energy is -> " + E.toExponential(4) + "\n";

     document.body.appendChild(span);

    }

}

window.onload = () =>

{

    var obj = new ToExponential_Method();

    var Mass = parseFloat(prompt("Enter a mass in Kg."));

    obj.toExponential(Mass);   

};

 

toExponential_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="toExponential.js"></script>

</head>

<body>

    <h3 style="color:blueviolet">toExponential() Number Object Method In TypeScript</h3>

    <div id="content"/>

</body>

</html>

 

toExponential.js

var ToExponential_Method = (function () {

    function ToExponential_Method() { }

    ToExponential_Method.prototype.toExponential = function (Mass) {

        var c = 2.99792458;

        var E = Mass * c * c;

        var span = document.createElement("span");

        span.innerText = "toExponential Method \n The amount of energy is -> " + E.toExponential(4) + "\n";

        document.body.appendChild(span);

    };

    return ToExponential_Method;

})();

window.onload = function () {

    var obj = new ToExponential_Method();

    var Mass = parseFloat(prompt("Enter a mass in Kg."));

    obj.toExponential(Mass);

};

//@ sourceMappingURL=toExponential.js.map

    

Output 1

Enter a mass in kg.


enter-mass.gif

 

Output 2


result.gif

 

Nitin Bhardwaj

Normal 0 false false false EN-US X-NONE X-NONE ... Read more

COMMENT USING

Trending up