diff options
| author | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
|---|---|---|
| committer | Peter Bengtsson <mail@peterbe.com> | 2020-12-08 14:42:17 -0500 |
| commit | da78a9e329e272dedb2400b79a3bdeebff387d47 (patch) | |
| tree | e6ef8aa7c43556f55ddfe031a01cf0a8fa271bfe /files/it/web/javascript/reference/global_objects/number | |
| parent | 1109132f09d75da9a28b649c7677bb6ce07c40c0 (diff) | |
| download | translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.gz translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.tar.bz2 translated-content-da78a9e329e272dedb2400b79a3bdeebff387d47.zip | |
initial commit
Diffstat (limited to 'files/it/web/javascript/reference/global_objects/number')
| -rw-r--r-- | files/it/web/javascript/reference/global_objects/number/index.html | 188 |
1 files changed, 188 insertions, 0 deletions
diff --git a/files/it/web/javascript/reference/global_objects/number/index.html b/files/it/web/javascript/reference/global_objects/number/index.html new file mode 100644 index 0000000000..39c8eb37d2 --- /dev/null +++ b/files/it/web/javascript/reference/global_objects/number/index.html @@ -0,0 +1,188 @@ +--- +title: Number +slug: Web/JavaScript/Reference/Global_Objects/Number +translation_of: Web/JavaScript/Reference/Global_Objects/Number +--- +<ol> + <li>{{JSRef}}</li> +</ol> + +<div><strong><code>Number</code></strong> is a <a href="/en-US/docs/Glossary/Primitive#Primitive_wrapper_objects_in_JavaScript">primitive wrapper object</a> used to represent and manipulate numbers like <code>37</code> or <code>-9.25</code>.</div> + +<div></div> + +<div></div> + +<p>The <strong><code>Number</code></strong> constructor contains constants and methods for working with numbers. Values of other types can be converted to numbers using the<strong> </strong><strong><code>Number()</code> function</strong>.</p> + +<p>The JavaScript <strong>Number</strong> type is a <a href="https://en.wikipedia.org/wiki/Floating-point_arithmetic">double-precision 64-bit binary format IEEE 754</a> value, like <code>double</code> in Java or C#. This means it can represent fractional values, but there are some limits to what it can store. A Number only keeps about 17 decimal places of precision; arithmetic is subject to <a href="https://en.wikipedia.org/wiki/Floating-point_arithmetic#Representable_numbers,_conversion_and_rounding">rounding</a>. The largest value a Number can hold is about 1.8×10<sup>308</sup>. Numbers beyond that are replaced with the special Number constant {{jsxref("Infinity")}}.</p> + +<p>A number literal like <code>37</code> in JavaScript code is a floating-point value, not an integer. There is no separate integer type in common everyday use. (JavaScript now has a {{jsxref("BigInt")}} type, but it was not designed to replace Number for everyday uses. <code>37</code> is still a Number, not a BigInt.)</p> + +<dl> +</dl> + +<h2 id="Description">Description</h2> + +<p>When used as a function, <code>Number(value)</code> converts a string or other value to the Number type. If the value can't be converted, it returns {{jsxref("NaN")}}.</p> + +<h3 id="Literal_syntax">Literal syntax</h3> + +<pre class="brush: js notranslate">123 // one-hundred twenty-three +123.0 // same +123 === 123.0 // true</pre> + +<h3 id="Function_syntax">Function syntax</h3> + +<pre class="brush: js notranslate">Number('123') // returns the number 123 +Number('123') === 123 // true + +Number("unicorn") // NaN +Number(undefined) // NaN +</pre> + +<h2 id="Constructor">Constructor</h2> + +<dl> + <dt><a href="/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/Number"><code>Number()</code></a></dt> + <dd>Creates a new <code>Number</code> value.</dd> +</dl> + +<h2 id="Static_properties">Static properties</h2> + +<dl> + <dt>{{jsxref("Number.EPSILON")}}</dt> + <dd>The smallest interval between two representable numbers.</dd> + <dt>{{jsxref("Number.MAX_SAFE_INTEGER")}}</dt> + <dd>The maximum safe integer in JavaScript (<code>2<sup>53</sup> - 1</code>).</dd> + <dt>{{jsxref("Number.MAX_VALUE")}}</dt> + <dd>The largest positive representable number.</dd> + <dt>{{jsxref("Number.MIN_SAFE_INTEGER")}}</dt> + <dd>The minimum safe integer in JavaScript (<code>-(2<sup>53</sup> - 1)</code>).</dd> + <dt>{{jsxref("Number.MIN_VALUE")}}</dt> + <dd>The smallest positive representable number—that is, the positive number closest to zero (without actually being zero).</dd> + <dt>{{jsxref("Number.NaN")}}</dt> + <dd>Special "<strong>N</strong>ot <strong>a</strong> <strong>N</strong>umber" value.</dd> + <dt>{{jsxref("Number.NEGATIVE_INFINITY")}}</dt> + <dd>Special value representing negative infinity. Returned on overflow.</dd> + <dt>{{jsxref("Number.POSITIVE_INFINITY")}}</dt> + <dd>Special value representing infinity. Returned on overflow.</dd> + <dt>{{jsxref("Number.prototype")}}</dt> + <dd>Allows the addition of properties to the <code>Number</code> object.</dd> +</dl> + +<h2 id="Static_methods">Static methods</h2> + +<dl> + <dt>{{jsxref("Number.isNaN()")}}</dt> + <dd>Determine whether the passed value is <code>NaN</code>.</dd> + <dt>{{jsxref("Number.isFinite()")}}</dt> + <dd>Determine whether the passed value is a finite number.</dd> + <dt>{{jsxref("Number.isInteger()")}}</dt> + <dd>Determine whether the passed value is an integer.</dd> + <dt>{{jsxref("Number.isSafeInteger()")}}</dt> + <dd>Determine whether the passed value is a safe integer (number between <code>-(2<sup>53</sup> - 1)</code> and <code>2<sup>53</sup> - 1</code>).</dd> + <dt>{{jsxref("Number.parseFloat()", "Number.parseFloat(<var>string</var>)")}}</dt> + <dd>This is the same as the global {{jsxref("parseFloat", "parseFloat()")}} function.</dd> + <dt>{{jsxref("Number.parseInt()", "Number.parseInt(<var>string</var>, [<var>radix</var>])")}}</dt> + <dd>This is the same as the global {{jsxref("parseInt", "parseInt()")}} function.</dd> +</dl> + +<h2 id="Instance_methods">Instance methods</h2> + +<dl> + <dt>{{jsxref("Number.prototype.toExponential()" ,"Number.prototype.toExponential(<var>fractionDigits</var>)")}}</dt> + <dd>Returns a string representing the number in exponential notation.</dd> + <dt>{{jsxref("Number.prototype.toFixed()", "Number.prototype.toFixed(<var>digits</var>)")}}</dt> + <dd>Returns a string representing the number in fixed-point notation.</dd> + <dt>{{jsxref("Number.prototype.toLocaleString()", "Number.prototype.toLocaleString([<var>locales</var> [, <var>options</var>]])")}}</dt> + <dd>Returns a string with a language sensitive representation of this number. Overrides the {{jsxref("Object.prototype.toLocaleString()")}} method.</dd> + <dt>{{jsxref("Number.prototype.toPrecision()", "Number.prototype.toPrecision(<var>precision</var>)")}}</dt> + <dd>Returns a string representing the number to a specified precision in fixed-point or exponential notation.</dd> + <dt>{{jsxref("Number.prototype.toString()", "Number.prototype.toString([<var>radix</var>])")}}</dt> + <dd>Returns a string representing the specified object in the specified <var>radix</var> ("base"). Overrides the {{jsxref("Object.prototype.toString()")}} method.</dd> + <dt>{{jsxref("Number.prototype.valueOf()")}}</dt> + <dd>Returns the primitive value of the specified object. Overrides the {{jsxref("Object.prototype.valueOf()")}} method.</dd> +</dl> + +<h2 id="Examples">Examples</h2> + +<h3 id="Using_the_Number_object_to_assign_values_to_numeric_variables">Using the Number object to assign values to numeric variables</h3> + +<p>The following example uses the <code>Number</code> object's properties to assign values to several numeric variables:</p> + +<pre class="brush: js notranslate">const biggestNum = Number.MAX_VALUE +const smallestNum = Number.MIN_VALUE +const infiniteNum = Number.POSITIVE_INFINITY +const negInfiniteNum = Number.NEGATIVE_INFINITY +const notANum = Number.NaN +</pre> + +<h3 id="Integer_range_for_Number">Integer range for Number</h3> + +<p>The following example shows the minimum and maximum integer values that can be represented as <code>Number</code> object. (More details on this are described in the ECMAScript standard, chapter <em><a href="https://tc39.github.io/ecma262/#sec-ecmascript-language-types-number-type">6.1.6 The Number Type</a>.</em>)</p> + +<pre class="brush: js notranslate">const biggestInt = Number.MAX_SAFE_INTEGER // (<code>2<sup>53</sup> - 1</code>) => 9007199254740991 +const smallestInt = Number.MIN_SAFE_INTEGER // -(<code>2<sup>53</sup> - 1</code>) => -9007199254740991</pre> + +<p>When parsing data that has been serialized to JSON, integer values falling outside of this range can be expected to become corrupted when JSON parser coerces them to <code>Number</code> type.</p> + +<p>A possible workaround is to use {{jsxref("String")}} instead.</p> + +<p>Larger numbers can be represented using the {{jsxref("BigInt")}} type.</p> + +<h3 id="Using_Number_to_convert_a_Date_object">Using Number to convert a Date object</h3> + +<p>The following example converts the {{jsxref("Date")}} object to a numerical value using <code>Number</code> as a function:</p> + +<pre class="brush: js notranslate">let d = new Date('December 17, 1995 03:24:00') +console.log(Number(d)) +</pre> + +<p>This logs <code>819199440000</code>.</p> + +<h3 id="Convert_numeric_strings_and_null_to_numbers">Convert numeric strings and null to numbers</h3> + +<pre class="brush: js notranslate" dir="rtl">Number('123') // 123 +Number('123') === 123 /// true +Number('12.3') // 12.3 +Number('12.00') // 12 +Number('123e-1') // 12.3 +Number('') // 0 +Number(null) // 0 +Number('0x11') // 17 +Number('0b11') // 3 +Number('0o11') // 9 +Number('foo') // NaN +Number('100a') // NaN +Number('-Infinity') //-Infinity</pre> + +<h2 id="Specifications">Specifications</h2> + +<table class="standard-table"> + <thead> + <tr> + <th scope="col">Specification</th> + </tr> + </thead> + <tbody> + <tr> + <td>{{SpecName('ESDraft', '#sec-number-objects', 'Number')}}</td> + </tr> + </tbody> +</table> + +<h2 id="Browser_compatibility">Browser compatibility</h2> + +<p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> + +<p>{{Compat("javascript.builtins.Number")}}</p> + +<h2 id="See_also">See also</h2> + +<ul> + <li>{{jsxref("NaN")}}</li> + <li>{{jsxref("Arithmetic operators")}}</li> + <li>The {{jsxref("Math")}} global object</li> + <li>Integers with arbitrary precision: {{jsxref("BigInt")}}</li> +</ul> |
