| home / programming / java_core / 2 | [previous] [next] |
|
|
Now that we've travelled this far in JavaScript, have you wondered how to format a floating-point number when you display it, as you can with the printf function in C or Perl? Well, the Number object, like the String object, gives you properties and methods to handle and customize numeric data. The Number object is a wrapper for the primitive numeric values (see Chapter 2, ), which means you can use a primitive number type or an object number type and JavaScript manages the conversion back and forth as necessary. The Number object was introduced in JavaScript 1.1.
The Number() constructor takes a numeric value as its argument. If used as a function, without the new operator, the argument is converted to a primitive numeric value, and that number is returned; if it fails, NaN is returned. The Number object has a number of properties and methods, as listed in Tables 9.11 and 9.12.
|
var number = new Number(numeric value); |
|
Used to customize the Number object by adding new properties and methods |
|
Converts a number to a string using a specified base (radix) |
|
|
Converts a number to a string using local number conventions |
|
|
toFixed()5 |
Converts a number to a string with a specified number of places after the decimal point |
|
Converts a number to a string using exponential notation and a specified number of places after the decimal point |
|
|
Converts a number to a string in either exponential or fixed notation containing the specified number of places after the decimal point |
The constants MAX_VALUE, MIN_VALUE, NEGATIVE_INFINITY, POSITIVE_INFINITY, and NaN, are properties of the Number() function, but are not used with instances of the Number object; thus, var huge = Number.MAX_VALUE is valid, but huge.MAX_VALUE is not. NaN is a special value that is returned when some mathematical operation results in a value that is not a number.
The methods provided to the Number object manipulate instances of number objects. For example, to convert numbers to strings representing different bases, the toString() method manipulates a number, either primitive or object. See See .
|
|
|---|
|
<head><title>Number Contants</title> <body bgcolor=orange><font color="black" size="+1"> <script language="JavaScript"> 1 var largest = Number.MAX_VALUE; 2 var smallest = Number.MIN_VALUE; 3 var num1 = 20; // A primitive numeric value 4 var num2 = new Number(13); // Creating a Number object document.write("<b>The largest number is " + largest+ "<br>"); document.write("The smallest number is "+ smallest + "<br>"); |
|
5 document.write("The number as a string (base 2): "+ 6 document.write("<br>The number as a string (base 8): "+ |
|
|
|---|
The constant MAX_VALUE is a property of the Number() function. This constant cannot be used with an instance of a Number object. |
|
| home / programming / java_core / 2 | [previous] [next] |
| ||||||||||||||||||||
Created: March 27, 2003
Revised: November 19, 2003
URL: http://webreference.com/programming/java_core/2