JavaScript手册 
目录
此参考中包含
的内容
轻松上手
简介
操作符
语句
核心
文档
窗口
表单
浏览器
事件和
事件句柄
LiveWire
数据库服务
进程管理服务
实用工具
全局函数
LiveConnect
的Java包
索引
版权
 
【目录】 【上一页】 【下一页】 【索引】

Number

Lets you work with numeric values. The Number object is an object wrapper for primitive numeric values.

Core object
实现版本Navigator 3.0, LiveWire 1.0
Navigator 4.0: modified behavior of Number constructor

创建源

The Number constructor:

new Number(value);

参数

valueThe numeric value of the object being created.

描述

The primary uses for the Number object are:

The properties of 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

属性概览

MAX_VALUEThe largest represen表 number.
MIN_VALUEThe smallest represen表 number.
NaNSpecial "not a number" value.
NEGATIVE_INFINITYSpecial infinite value; returned on overflow.
POSITIVE_INFINITYSpecial negative infinite value; returned on overflow.
prototypeAllows the addition of properties to a Number object.

方法概览

toStringReturns a string representing the specified object.

示例

示例 1. The following example uses the Number object's properties to assign values to several numeric variables:

biggestNum = Number.MAX_VALUE
smallestNum = Number.MIN_VALUE
infiniteNum = Number.POSITIVE_INFINITY
negInfiniteNum = Number.NEGATIVE_INFINITY
notANum = Number.NaN
示例 2. The following example creates a Number object, myNum, then adds a描述 property to all Number objects. Then a value is assigned to the myNum object's描述 property.

myNum = new Number(65)
Number.prototype.description=null
myNum.description="wind speed"

属性

MAX_VALUE

The maximum numeric value represen表 in JavaScript.

属性源Number
静态, 只读
实现版本Navigator 3,0, LiveWire 1.0

描述

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 static property of Number, you always use it as Number.MAX_VALUE, rather than as a property of a Number object you created.

示例

The following 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 smallest positive numeric value represen表 in JavaScript.

属性源Number
静态, 只读
实现版本Navigator 3,0, LiveWire 1.0

描述

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

Because MIN_VALUE is a static property of Number, you always use it as Number.MIN_VALUE, rather than as a property of a Number object you created.

示例

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

A special value representing Not-A-Number. This value is represented as the unquoted literal NaN.

属性源Number
只读
实现版本Navigator 3,0, LiveWire 1.0

描述

JavaScript prints the value 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.

You might use the NaN property to indicate an error condition for a function that should return a valid number.

示例

In the following example, if 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

NEGATIVE_INFINITY

A special numeric value representing negative infinity. This value is displayed as "-Infinity".

属性源Number
静态, 只读
实现版本Navigator 3,0, LiveWire 1.0

描述

This value behaves mathematically like infinity; for example, anything multiplied by infinity is infinity, and anything divided by infinity is 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.

示例

In the following example, the variable 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()

POSITIVE_INFINITY

A special numeric value representing infinity. This value is displayed as "Infinity".

属性源Number
静态, 只读
实现版本Navigator 3,0, LiveWire 1.0

描述

This value behaves mathematically like infinity; for example, anything multiplied by infinity is infinity, and anything divided by infinity is 0.

JavaScript does not have a literal for Infinity.

Because POSITIVE_INFINITY is a static property of Number, you always use it as Number.POSITIVE_INFINITY, rather than as a property of a Number object you created.

示例

In the following example, the variable 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()

prototype

Represents the prototype for this class. You can use the prototype to add properties or methods to all instances of a class. For information on prototypes, see Function.prototype.

属性源Number
实现版本Navigator 3.0, LiveWire 1.0

方法

toString

Returns a string representing the specified object.

方法源Number
实现版本Navigator 3.0

语法

toString()
toString(radix)

参数

radix(Optional) An integer between 2 and 16 specifying the base to use for representing numeric values.

描述

Every object has a 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 error
document.write("45.toString() is " + 45.toString() + "<BR>")
For information on defining your own toString method, see the Object.toString method.


【目录】 【上一页】 【下一页】 【索引】

回页面顶部回页面顶部