Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

The Number Object

The Number object is a built-in JavaScript object, very similar to the Math object in that it encapsulates several primitive numeric values. It is different from the Math object in that Number object is a dynamic one requiring a creation step, while Math object is a static one, not requiring any instantiation.

The primary use for the Number object is to access its constant properties, including the largest and smallest representable numbers, positive and negative infinity, and the Not-a-Number value. You can also use the Number object to create numeric objects that you can add properties to. It is unlikely that you will need to use the Number object, and it is given here for sake of completion.

To create a Number object, use the following statement:

numberObjectName = new Number()

where numberObjectName is either the name of a new object or a property of an existing one. To access Number’s properties, use the following format:

numberObjectName.propertyName

where numberObjectName is either the name of an existing Number object or a property of an existing object. PropertyName is one of the properties listed below:

MAX_VALUE

This is the maximum numeric value representable in JavaScript. The MAX_VALUE property has a value of approximately 1.79E+308. Values larger than MAX_VALUE are represented as “infinity.” Because MAX_VALUE is a constant, it is a read-only property of Number. The following code demonstrates the use of MAX_VALUE. The code multiplies two numeric values. If the result is less than or equal to MAX_VALUE, the func1 function is called; otherwise, the func2 function is called:

if (num1 * num2 <= Number.MAX_VALUE)
   func1()
else
   func2()

MIN_VALUE

The MIN_VALUE property is the number closest to zero, not the most negative number, that JavaScript can represent. MIN_VALUE has a value of approximately 2.22E–308. Values smaller than MIN_VALUE (“underflow values”) are converted to zero. Because MIN_VALUE is a constant, it is a read-only property of Number. The following code divides two numeric values. If the result is greater than or equal to MIN_VALUE, the func1 function is called; otherwise, the func2 function is called.

if (num1 / num2 >= Number.MIN_VALUE)
   func1()
else
   func2()

NaN

Unquoted literal constant NaN is a special value representing Not-A-Number. Since NaN always compares unequal to any number, including NaN, it is usually used to indicate an error condition for a function that should return a valid number. Notice, then, that you cannot check for Not-a-Number value by comparing to Number.NaN. Use the isNaN() function instead. Because NaN is a constant, it is a read-only property of Number. In the following code segment, dayOfMonth is assigned NaN if it is greater than 31, and a message is displayed indicating the valid range:

if (dayOfMonth < 1 || dayOfMonth > 31) {
   dayOfMonth = Number.NaN
   alert("Day of Month must be between 1 and 31.")
}

NEGATIVE_INFINITY

This is a special numeric value representing negative infinity. This value is represented as “–Infinity.” This value resembles an infinity in its mathematical behavior. For example, anything multiplied by NEGATIVE_INFINITY is NEGATIVE_INFINITY, and anything divided by NEGATIVE_INFINITY is zero. Because NEGATIVE_ INFINITY is a constant, it is a read-only property of Number.

The following code extract checks a number for NEGATIVE_ INFINITY and calls a different function if it is:

if (smallNumber == Number.NEGATIVE_INFINITY)
   func1()
else
   func2()

POSITIVE_INFINITY

This is a special numeric value representing infinity. This value is represented as “ Infinity.” This value resembles an infinity in its mathematical behavior. For example, anything multiplied by POSITIVE_INFINITY is POSITIVE_INFINITY, and anything divided by POSITIVE_INFINITY is zero. Because POSITIVE_INFINITY is a constant, it is a read-only property of Number.

The following code extract checks a number for POSITIVE_INFINITY and calls a different function if it is:

if (bigNumber == Number.POSITIVE_INFINITY)
   func1()
else
   func2()

Number Methods

The Number object has no specific methods. You can still use the generic methods eval(), toString(), and valueOf(), which are applicable to every object.

Math-Related Functions

Although the functions presented in this section are also discussed later in the book, it is important that you attain a basic understanding, so do not skip the following explanation. These functions are used in examples later in the chapter.

parseInt()

This built-in function accepts a numeric string of an integer and returns its corresponding numeric value. The following example is worth a thousand words:

var numStr = "99"
document.write("The initial " + typeof numStr + " is " + numStr)
document.write("<BR>")
var num = parseInt(numStr)
document.write("The converted " + typeof num + " is " + num)

The script’s output is:

The initial string is 99
The converted number is 99

The data types are not provided to the document.write() method as literals, but as values. Note that the parseInt() function accepts a string and returns an integer. If a noninteger numeric string is given to the function, it returns the nearest integer, as a number (as opposed to a string).

parseFloat()

This function is exactly the same as its preceding one, except that it does not round off any number. It converts the numeric string to a number, without changing the numeric value at all. You should use this function with floating point numeric strings, or when you are not sure what type of number is being used.

eval()

The eval() function is both powerful and useful. For now, you just need to know that upon acceptance of a string representing a numeric value or mathematical expression, it returns the value which the expression evaluates to. The following script segment prints “13”:

var str = "6 + 7"
document.write(eval(str))

The function returns a number, not a string. Note that this function acts differently on Netscape Navigator than it does on Microsoft Internet Explorer. An example for such different behavior is seen in the following statement, which works fine under Internet Explorer, but generates an error under Navigator:

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us