Number
object is an object wrapper for primitive numeric values.
Core object | |
Implemented in |
Navigator 3.0, LiveWire 1.0 Navigator 4.0: modified behavior of Number constructor
|
Number
constructor:new Number(value);
value | The numeric value of the object being created. |
Number
object are:Number
object.Number
are properties of the class itself, not of individual Number
objects.
Navigator 4.0: Number(x)
now produces NaN
rather than an error if x
is a string that does not contain a well-formed numeric literal. For example,
x=Number("three");
document.write(x + "<BR>");prints
NaN
| Returns a string representing the specified object. |
Number
object's properties to assign values to several numeric variables:biggestNum = Number.MAX_VALUEExample 2. The following example creates a
smallestNum = Number.MIN_VALUE
infiniteNum = Number.POSITIVE_INFINITY
negInfiniteNum = Number.NEGATIVE_INFINITY
notANum = Number.NaN
Number
object, myNum
, then adds a description
property to all Number
objects. Then a value is assigned to the myNum
object's description
property.myNum = new Number(65)
Number.prototype.description=null
myNum.description="wind speed"
Property of |
Number
|
Static, Read-only | |
Implemented in | Navigator 3,0, LiveWire 1.0 |
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 static property of Number
, you always use it as Number.MAX_VALUE
, rather than as a property of a Number
object you created.
MAX_VALUE
, the func1
function is called; otherwise, the func2
function is called.if (num1 * num2 <= Number.MAX_VALUE)
func1()
else
func2()
Property of |
Number
|
Static, Read-only | |
Implemented in | Navigator 3,0, LiveWire 1.0 |
MIN_VALUE
property is the number closest to 0, 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 0.
MIN_VALUE
, the func1
function is called; otherwise, the func2
function is called.if (num1 / num2 >= Number.MIN_VALUE)
func1()
else
func2()
Property of |
Number
|
Read-only | |
Implemented in | Navigator 3,0, LiveWire 1.0 |
Number.NaN
as NaN
.
NaN
is always unequal to any other number, including NaN itself; you cannot check for the not-a-number value by comparing to Number.NaN
. Use the isNaN
function instead.
month
has a value greater than 12, it is assigned NaN, and a message is displayed indicating valid values.var month = 13
if (month < 1 || month > 12) {
month = Number.NaN
alert("Month must be between 1 and 12.")
}
isNaN
, parseFloat
, parseInt
"-Infinity"
.
Property of |
Number
|
Static, Read-only | |
Implemented in | Navigator 3,0, LiveWire 1.0 |
Because NEGATIVE_INFINITY
is a static property of Number
, you always use it as Number.NEGATIVE_INFINITY
, rather than as a property of a Number
object you created.
smallNumber
is assigned a value that is smaller than the minimum value. When the if
statement executes, smallNumber
has the value "-Infinity"
, so the func1
function is called.var smallNumber = -Number.MAX_VALUE*10
if (smallNumber == Number.NEGATIVE_INFINITY)
func1()
else
func2()
"Infinity"
.
Property of |
Number
|
Static, Read-only | |
Implemented in | Navigator 3,0, LiveWire 1.0 |
JavaScript does not have a literal for Infinity.
bigNumber
is assigned a value that is larger than the maximum value. When the if
statement executes, bigNumber
has the value "Infinity"
, so the func1
function is called.var bigNumber = Number.MAX_VALUE * 10
if (bigNumber == Number.POSITIVE_INFINITY)
func1()
else
func2()
Function.prototype
.
Property of |
Number
|
Implemented in | Navigator 3.0, LiveWire 1.0 |
Method of |
Number
|
Implemented in | Navigator 3.0 |
toString()
toString(radix)
radix | (Optional) An integer between 2 and 16 specifying the base to use for representing numeric values. |
toString
method that is automatically called when it is to be represented as a text value or when an object is referred to in a string concatenation.
You can use toString
within your own code to convert an object into a string, and you can create your own function to be called in place of the default toString
method.
You can use toString
on numeric values, but not on numeric literals:
// The next two lines are valid
var howMany=10
document.write("howMany.toString() is " + howMany.toString() + "<BR>")
// The next line causes an errorFor information on defining your own
document.write("45.toString() is " + 45.toString() + "<BR>")
toString
method, see the Object.toString
method.
Last Updated: 10/31/97 12:30:31