Number

  • Revision slug: JavaScript/Reference/Global_Objects/Number
  • Revision title: Number
  • Revision id: 49301
  • Created:
  • Creator: Erik1
  • Is current revision? No
  • Comment /* Methods */

Revision Content

Summary

Core Object

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

Created by

The Number constructor:

new Number(value)

Parameters

value 
The numeric value of the object being created.

Description

The primary uses for the Number object are:

  • To access its constant properties, which represent the largest and smallest representable numbers, positive and negative infinity, and the Not-a-Number value.
  • To create numeric objects that you can add properties to. Most likely, you will rarely need to create a Number object.

The properties of Number are properties of the class itself, not of individual Number objects.

JavaScript 1.2: 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, the following prints NaN:

x=Number("three");
document.write(x + "<BR>");

You can convert any object to a number using the top-level Number function.

Properties

constructor: Specifies the function that creates an object's prototype.

MAX_VALUE: The largest representable number.

MIN_VALUE: The smallest representable number.

NaN: Special "not a number" value.

NEGATIVE_INFINITY: Special value representing negative infinity; returned on overflow.

POSITIVE_INFINITY: Special value representing infinity; returned on overflow.

prototype: Allows the addition of properties to a Number object.

Methods

toExponential: Returns a string representing the number in exponential notation.

toFixed: Returns a string representing the number in fixed-point notation.

toLocaleString: Returns a human readable string representing the number using the locale of the environment.

toPrecision: Returns a string representing the number to a specified precision in fixed-point notation.

toSource: Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the Object.toSource method.

toString: Returns a string representing the specified object. Overrides the Object.toString method.

valueOf: Returns the primitive value of the specified object. Overrides the Object.valueOf method.

In addition, this object inherits the watch and unwatch methods from Object.

Examples

Example: Using the Number object to assign values to numeric variables

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;

Example: Using Number object to modify all Number objects

The following example creates a 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";

Revision Source

<p>
</p>
<h3 name="Summary"> Summary </h3>
<p><b>Core Object</b>
</p><p>Lets you work with numeric values.  The <code>Number</code> object is an object wrapper for primitive numeric values.
</p>
<h3 name="Created_by"> Created by </h3>
<p>The <code>Number</code> constructor:
</p>
<pre class="eval">new Number(<i>value</i>)
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>value</code> </dt><dd> The numeric value of the object being created.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>The primary uses for the <code>Number</code> object are:
</p>
<ul><li> To access its constant properties, which represent the largest and smallest representable numbers, positive and negative infinity, and the Not-a-Number value.
</li></ul>
<ul><li> To create numeric objects that you can add properties to. Most likely, you will rarely need to create a <code>Number</code> object.
</li></ul>
<p>The properties of <code>Number</code> are properties of the class itself, not of individual <code>Number</code> objects.
</p><p><b>JavaScript 1.2</b>: <code>Number(x)</code> now produces <code>NaN</code> rather than an error if <code>x</code> is a string that does not contain a well-formed numeric literal. For example, the following prints <code>NaN</code>:
</p>
<pre>x=Number("three");
document.write(x + "&lt;BR&gt;");
</pre>
<p>You can convert any object to a number using the top-level <a href="en/Core_JavaScript_1.5_Reference/Functions/Number">Number</a> function.
</p>
<h3 name="Properties"> Properties </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/constructor">constructor</a>: Specifies the function that creates an object's prototype.  
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/MAX_VALUE">MAX_VALUE</a>: The largest representable number.  
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/MIN_VALUE">MIN_VALUE</a>: The smallest representable number.  
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/NaN">NaN</a>: Special "not a number" value.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/NEGATIVE_INFINITY">NEGATIVE_INFINITY</a>: Special value representing negative infinity; returned on overflow.  
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/POSITIVE_INFINITY">POSITIVE_INFINITY</a>: Special value representing infinity; returned on overflow.  
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/prototype">prototype</a>: Allows the addition of properties to a Number object.  
</p>
<h3 name="Methods"> Methods </h3>
<p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toExponential">toExponential</a>: Returns a string representing the number in exponential notation.  
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toFixed">toFixed</a>: Returns a string representing the number in fixed-point notation.  
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toLocaleString">toLocaleString</a>:
Returns a human readable string representing the number using the locale of the environment.
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toPrecision">toPrecision</a>: Returns a string representing the number to a specified precision in fixed-point notation.  
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toSource">toSource</a>: Returns an object literal representing the specified Number object; you can use this value to create a new object. Overrides the <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toSource">Object.toSource</a> method.  
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/toString">toString</a>: Returns a string representing the specified object. Overrides the <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/toString">Object.toString</a> method.  
</p><p><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Number/valueOf">valueOf</a>: Returns the primitive value of the specified object. Overrides the <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/valueOf">Object.valueOf</a> method.
</p><p>In addition, this object inherits the <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/watch">watch</a> and <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object/unwatch">unwatch</a> methods from <a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Object">Object</a>.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_the_Number_object_to_assign_values_to_numeric_variables"> Example: Using the <code>Number</code> object to assign values to numeric variables </h4>
<p>The following example uses the <code>Number</code> object's properties to assign values to several numeric variables:
</p>
<pre class="eval">biggestNum = Number.MAX_VALUE;
smallestNum = Number.MIN_VALUE;
infiniteNum = Number.POSITIVE_INFINITY;
negInfiniteNum = Number.NEGATIVE_INFINITY;
notANum = Number.NaN;
</pre>
<h4 name="Example:_Using_Number_object_to_modify_all_Number_objects"> Example: Using <code>Number</code> object to modify all <code>Number</code> objects </h4>
<p>The following example creates a Number object, myNum, then adds a description property to all Number objects. Then a value is assigned to the myNum object's description property.
</p>
<pre class="eval">myNum = new Number(65);
Number.prototype.description = null;
myNum.description = "wind speed";
</pre>
Revert to this revision