Using the Number Object


Using the Number Object

You can create a new Number object with the Number constructor like this:

 var number1 = new Number(5) 

That's all it takes, now you can use the Number object's properties and methods . I'll take a look at a few of them here.

Setting Precision

You can set the precision of numeric values with the setPrecision method, which sets the total number of significant digits (before and after the decimal place). We've already seen this method at work in Chapter 12, "Working with Forms, Buttons, Check Boxes, and Radio Buttons," when we let the user click radio buttons and check boxes to select the price of a sandwich. All prices were of the form n.nn that is, three significant digits; so to make sure the price had two decimal places, we used setPrecision(3) , as you can see (Listing 12-14.html):

 function displayCost()  {      var cost = 0      for (var loopIndex = 0; loopIndex < document.form1.elements.length; loopIndex++) {          if(document.form1.elements[loopIndex].type=="checkbox"              && document.form1.elements[loopIndex].checked){              cost += .50          }      }  document.getElementById("div1").innerHTML = "Total cost: $" + cost.toPrecision(3)  } 

This worked because every price had three significant digits. If the price could be any number of digits, however, we can ensure two decimal places with the toFixed method, which explicitly sets the number of decimal places, like this:

 function displayCost()  {      var cost = 0      for (var loopIndex = 0; loopIndex < document.form1.elements.length; loopIndex++) {          if(document.form1.elements[loopIndex].type=="checkbox"              && document.form1.elements[loopIndex].checked){              cost += .50          }      }  document.getElementById("div1").innerHTML = "Total cost: $" + cost.toFixed(2)  } 

Determining Maximum and Minimum Possible Values

JavaScript is implemented on many different machines and a number of different platforms. If you need to know how big or how small the numbers you can handle in your code can be, check the MAX_VALUE and MIN_VALUE properties of the Number object. (These properties are in capital letters to explicitly indicate that they're constants.) Here's an example that does that, displaying the maximum and minimum numbers you can handle in JavaScript for a particular browser:

(Listing 19-03.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Maximum and Minimum Values          </TITLE>      </HEAD>      <BODY>          <H1>Maximum and Minimum Values</H1>          <FORM NAME="form1">              <INPUT TYPE="TEXT" NAME="text1" SIZE="40">              <BR>              <INPUT TYPE="TEXT" NAME="text2" SIZE="40">          </FORM>          <SCRIPT LANGUAGE="JavaScript">              <!--             document.form1.text1.value = "Max value = " + Number.MAX_VALUE              document.form1.text2.value = "Min value = " + Number.MIN_VALUE              // -->          </SCRIPT>      </BODY>  </HTML> 

You can see the results in Figure 19.3, where you see the maximum and minimum possible values for the Internet Explorer's JScript implementation.

Figure 19.3. Ascertaining maximum and minimum possible numeric values.

graphics/19fig03.gif

Using the prototype Property

One of the most useful properties of the Number object is the prototype property, which enables you to add your own methods to Number objects (just like the prototype property of String objects). When you add a method to a Number object in this way, it's added to all Number objects.

Here's an example. Suppose that I want to add a method named sqrt to all Number objects that returned the square root of the number. In this case, you could use that method like this:

 var n = new Number(5)  var squareRoot = n.sqrt() 

Here's how to do that. I start by adding a prototype named sqrt to all Number objects and tying that prototype to a function named sqRoot :

 Number.prototype.sqrt = sqRoot          .          .          . 

In the sqRoot function, I can refer to the current Number object with the this keyword, and the value stored in the current Number object as this.valueOf() . That means I can return the square root of the current number like this in the sqRoot function:

 Number.prototype.sqrt = sqRoot  function sqRoot()  {  return Math.sqrt(this.valueOf())  } 

Here's how that looks in an example, where the user can enter a number in a text field and click a button to see the square root of that number:

(Listing 19-04.html on the web site)
 <HTML>      <HEAD>          <TITLE>              Using the prototype Property          </TITLE>          <SCRIPT LANGUAGE="JavaScript">              <!--             Number.prototype.sqrt = sqRoot              function sqRoot()              {                  return Math.sqrt(this.valueOf())              }              function display()              {                  var n = new Number(document.form1.text1.value)                  document.form1.text1.value = n.sqrt()              }              // -->          </SCRIPT>      </HEAD>      <BODY>          <H1>Using the prototype Property</H1>          <FORM NAME="form1">              <INPUT TYPE="TEXT" NAME="text1">              <INPUT TYPE="BUTTON" ONCLICK="display()" VALUE="Get the Square Root">          </FORM>      </BODY>  </HTML> 

You can see the results in Figure 19.4, where I'm finding the square root of 5.

Figure 19.4. Using the prototype property.

graphics/19fig04.gif

As you can imagine, the prototype property is very useful when you want to augment objects such as String or Number .



Inside Javascript
Inside JavaScript
ISBN: 0735712859
EAN: 2147483647
Year: 2005
Pages: 492
Authors: Steve Holzner

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net