From 218934fa2ed1c702a6d3923d2aa2cc6b43c48684 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:43:23 -0500 Subject: initial commit --- .../reference/global_objects/number/index.html | 219 +++++++++++++++++++++ .../global_objects/number/isfinite/index.html | 93 +++++++++ .../global_objects/number/isnan/index.html | 99 ++++++++++ .../global_objects/number/prototype/index.html | 84 ++++++++ .../global_objects/number/toexponential/index.html | 164 +++++++++++++++ .../global_objects/number/tofixed/index.html | 108 ++++++++++ 6 files changed, 767 insertions(+) create mode 100644 files/zh-tw/web/javascript/reference/global_objects/number/index.html create mode 100644 files/zh-tw/web/javascript/reference/global_objects/number/isfinite/index.html create mode 100644 files/zh-tw/web/javascript/reference/global_objects/number/isnan/index.html create mode 100644 files/zh-tw/web/javascript/reference/global_objects/number/prototype/index.html create mode 100644 files/zh-tw/web/javascript/reference/global_objects/number/toexponential/index.html create mode 100644 files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html (limited to 'files/zh-tw/web/javascript/reference/global_objects/number') diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/index.html new file mode 100644 index 0000000000..8a023d1603 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/index.html @@ -0,0 +1,219 @@ +--- +title: Number +slug: Web/JavaScript/Reference/Global_Objects/Number +tags: + - JavaScript + - NeedsTranslation + - Number + - Reference + - TopicStub +translation_of: Web/JavaScript/Reference/Global_Objects/Number +--- +
{{JSRef}}
+ +

Number JavaScript 物件是允許你操作數值的包覆物件. Number 物件是以 Number() 建構子來建立的。

+ +

語法

+ +
new Number(value);
+ +

參數

+ +
+
value
+
用來建立物件的數值。
+
+ +

說明

+ +

Number 物件主要的用途:

+ + + +

屬性

+ +
+
{{jsxref("Number.EPSILON")}}
+
介於1和大於1的最小值之可表示的差。
+
{{jsxref("Number.MAX_SAFE_INTEGER")}}
+
JavaScript 中 IEEE-754 雙精度範圍間的最大整數 (253 - 1) 。
+
{{jsxref("Number.MAX_VALUE")}}
+
可表示的最大正整數。
+
{{jsxref("Number.MIN_SAFE_INTEGER")}}
+
JavaScript 中 IEEE-754 雙精度範圍間的最小整數 (-(253 - 1)) 。
+
{{jsxref("Number.MIN_VALUE")}}
+
可表示的最小值,即最靠近0的正整數?(5.00×103245.00\times10^{324})。
+
{{jsxref("Number.NaN")}}
+
特別用來表示非數值的物件。
+
{{jsxref("Number.NEGATIVE_INFINITY")}}
+
特別用來表示負無窮的數值。
+
{{jsxref("Number.POSITIVE_INFINITY")}}
+
特別用來表示正無窮的數值。
+
{{jsxref("Number.prototype")}}
+
允許被添加到 Number 物件的屬性。
+
+ +

方法

+ +
+
{{jsxref("Number.isNaN()")}}
+
判斷傳入的值是不是 NaN.
+
{{jsxref("Number.isFinite()")}}
+
判斷傳入的值是不是一個有限的數值。
+
{{jsxref("Number.isInteger()")}}
+
判斷傳入的值是不是一個整數。
+
{{jsxref("Number.isSafeInteger()")}}
+
判斷傳入的值是不是在 IEEE-754 雙精度範圍間 (即介於 -(253 - 1) 和 253 - 1之前)。
+
{{jsxref("Number.toInteger()")}} {{obsolete_inline}}
+
被用來評估傳入的值且將其轉換成整數 (或 {{jsxref("Global_Objects/Infinity", "Infinity")}}) 但已被移除。
+
{{jsxref("Number.parseFloat()")}}
+
這個方法和全域物件的 {{jsxref("parseFloat", "parseFloat()")}} 相同。
+
{{jsxref("Number.parseInt()")}}
+
這個方法和全域物件的 {{jsxref("parseInt", "parseInt()")}} 相同。
+
+ +

Number 實體

+ +

所有 Number 實體都會繼承其建構式的 {{jsxref("Number.prototype")}}。Number 的原型物件可以被修改並作用在所有 Number 實體。

+ +

方法

+ +
{{page('/zh-TW/docs/Web/JavaScript/Reference/Global_Objects/Number/prototype', '方法')}}
+ +

範例

+ +

使用 Number 物件去指派值給數值變數

+ +

下列的範例使用 Number 物件的屬性去指派值給數個數值變數:

+ +
var biggestNum = Number.MAX_VALUE;
+var smallestNum = Number.MIN_VALUE;
+var infiniteNum = Number.POSITIVE_INFINITY;
+var negInfiniteNum = Number.NEGATIVE_INFINITY;
+var notANum = Number.NaN;
+
+ +

Number 的整數範圍

+ +

下面的範例展示了最小和最大的整數,其可以被表示成 Number 物件(細節請參考 ECMAScript standard, chapter 8.5 The Number Type):

+ +
var biggestInt = 9007199254740992;
+var smallestInt = -9007199254740992;
+
+ +

當在解析已經被序列化的 JSON 的資料時,填入這個範圍之外的整數並且 JSON 剖析器強制將其轉成 Number 型別造成損壞是可預期的。將範圍之外的正數換成以 {{jsxref("String")}} 表示反倒是一個可行的替代方案。

+ +

使用 Number 轉換 Date 物件為 Unix 時間戳記

+ +

下面的範例將 Number 視為函式,並且使用它將 {{jsxref("Date")}} 轉換成時間戳記:

+ +
var d = new Date('December 17, 1995 03:24:00');
+console.log(Number(d)); // 819199440000
+
+
+ +

轉換數值字串成數值

+ +
Number("123")     // 123
+Number("12.3")    // 12.3
+Number("")        // 0
+Number("0x11")    // 17
+Number("0b11")    // 3
+Number("0o11")    // 9
+Number("foo")     // NaN
+Number("100a")    // NaN
+
+ +

規範

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
規範狀態註記
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.1.
{{SpecName('ES5.1', '#sec-15.7', 'Number')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-number-objects', 'Number')}}{{Spec2('ES6')}}New methods and properties added: {{jsxref("Number.EPSILON", "EPSILON")}}, {{jsxref("Number.isFinite", "isFinite")}}, {{jsxref("Number.isInteger", "isInteger")}}, {{jsxref("Number.isNaN", "isNaN")}}, {{jsxref("Number.parseFloat", "parseFloat")}}, {{jsxref("Number.parseInt", "parseInt")}}
{{SpecName('ESDraft', '#sec-number-objects', 'Number')}}{{Spec2('ESDraft')}} 
+ +

瀏覽器相容

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

參見

+ + diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/isfinite/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/isfinite/index.html new file mode 100644 index 0000000000..a15d29c7c0 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/isfinite/index.html @@ -0,0 +1,93 @@ +--- +title: Number.isFinite() +slug: Web/JavaScript/Reference/Global_Objects/Number/isFinite +tags: + - JavaScript + - Method + - Number + - Reference +translation_of: Web/JavaScript/Reference/Global_Objects/Number/isFinite +--- +
{{JSRef}}
+ +

Number.isFinite() 方法會判斷傳入的值是否為有限數(finite number)。

+ +
{{EmbedInteractiveExample("pages/js/number-isfinite.html")}}
+ + + +

語法

+ +
Number.isFinite(value)
+ +

參數

+ +
+
value
+
The value to be tested for finiteness.
+
+ +

回傳值

+ +

A {{jsxref("Boolean")}} indicating whether or not the given value is a finite number.

+ +

說明

+ +

In comparison to the global {{jsxref("isFinite", "isFinite()")}} function, this method doesn't forcibly convert the parameter to a number. This means only values of the type number, that are also finite, return true.

+ +

範例

+ +
Number.isFinite(Infinity);  // false
+Number.isFinite(NaN);       // false
+Number.isFinite(-Infinity); // false
+
+Number.isFinite(0);         // true
+Number.isFinite(2e64);      // true
+
+Number.isFinite('0');       // false, would've been true with
+                            // global isFinite('0')
+Number.isFinite(null);      // false, would've been true with
+                            // global isFinite(null)
+
+ +

Polyfill

+ +
Number.isFinite = Number.isFinite || function(value) {
+    return typeof value === 'number' && isFinite(value);
+}
+
+ +

規範

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES6', '#sec-number.isfinite', 'Number.isInteger')}}{{Spec2('ES6')}}Initial definition.
{{SpecName('ESDraft', '#sec-number.isfinite', 'Number.isInteger')}}{{Spec2('ESDraft')}} 
+ +

瀏覽器相容性

+ + + +

{{Compat("javascript.builtins.Number.isFinite")}}

+ +

參見

+ + diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/isnan/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/isnan/index.html new file mode 100644 index 0000000000..9209e40779 --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/isnan/index.html @@ -0,0 +1,99 @@ +--- +title: Number.isNaN() +slug: Web/JavaScript/Reference/Global_Objects/Number/isNaN +translation_of: Web/JavaScript/Reference/Global_Objects/Number/isNaN +--- +
{{JSRef}}
+ +

The Number.isNaN() method determines whether the passed value is {{jsxref("NaN")}} and its type is {{jsxref("Number")}}. It is a more robust version of the original, global {{jsxref("isNaN", "isNaN()")}}.

+ +
{{EmbedInteractiveExample("pages/js/number-isnan.html", "taller")}}
+ + + +

Syntax

+ +
Number.isNaN(value)
+ +

Parameters

+ +
+
value
+
The value to be tested for {{jsxref("NaN")}}.
+
+ +

Return value

+ +

true if the given value is {{jsxref("NaN")}} and its type is {{jsxref("Number")}}; otherwise, false.

+ +

Description

+ +

Due to both equality operators, {{jsxref("Operators/Comparison_Operators", "==", "#Equality")}} and {{jsxref("Operators/Comparison_Operators", "===", "#Identity")}}, evaluating to false when checking if {{jsxref("NaN")}} is {{jsxref("NaN")}}, the function Number.isNaN() has become necessary. This situation is unlike all other possible value comparisons in JavaScript.

+ +

In comparison to the global {{jsxref("isNaN", "isNaN()")}} function, Number.isNaN() doesn't suffer the problem of forcefully converting the parameter to a number. This means it is now safe to pass values that would normally convert to {{jsxref("NaN")}}, but aren't actually the same value as {{jsxref("NaN")}}. This also means that only values of the type number, that are also {{jsxref("NaN")}}, return true.

+ +

Examples

+ +
Number.isNaN(NaN);        // true
+Number.isNaN(Number.NaN); // true
+Number.isNaN(0 / 0);      // true
+
+// e.g. these would have been true with global isNaN()
+Number.isNaN('NaN');      // false
+Number.isNaN(undefined);  // false
+Number.isNaN({});         // false
+Number.isNaN('blabla');   // false
+
+// These all return false
+Number.isNaN(true);
+Number.isNaN(null);
+Number.isNaN(37);
+Number.isNaN('37');
+Number.isNaN('37.37');
+Number.isNaN('');
+Number.isNaN(' ');
+
+ +

Polyfill

+ +

The following works because NaN is the only value in javascript which is not equal to itself.

+ +
Number.isNaN = Number.isNaN || function(value) {
+    return value !== value;
+}
+
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES2015', '#sec-number.isnan', 'Number.isnan')}}{{Spec2('ES2015')}}Initial definition.
{{SpecName('ESDraft', '#sec-number.isnan', 'Number.isnan')}}{{Spec2('ESDraft')}} 
+ +

Browser compatibility

+ + + +

{{Compat("javascript.builtins.Number.isNaN")}}

+ +

See also

+ + diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/prototype/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/prototype/index.html new file mode 100644 index 0000000000..2a41977a2d --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/prototype/index.html @@ -0,0 +1,84 @@ +--- +title: Number.prototype +slug: Web/JavaScript/Reference/Global_Objects/Number/prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Number +--- +
{{JSRef}}
+ +

Number.prototype 屬性用來表示 {{jsxref("Number")}} 建構式的原型。

+ +
{{js_property_attributes(0, 0, 0)}}
+ +

說明

+ +

所有 {{jsxref("Number")}} 實體都繼承自 Number.prototype 。{{jsxref("Number")}} 建構式的原型物件可以被修改並作用在所有 {{jsxref("Number")}} 實體。

+ +

屬性

+ +
+
Number.prototype.constructor
+
回傳建立這個物件實體的建構式。預設為 {{jsxref("Number")}} 物件。
+
+ +

方法

+ +
+
{{jsxref("Number.prototype.toExponential()")}}
+
回傳以「科學記數法」表示的數值字串。
+
{{jsxref("Number.prototype.toFixed()")}}
+
回傳以定點表示的數值字串。
+
{{jsxref("Number.prototype.toLocaleString()")}}
+
回傳以當地語言為主的數值字串。這覆寫 {{jsxref("Object.prototype.toLocaleString()")}} 的方法。
+
{{jsxref("Number.prototype.toPrecision()")}}
+
回傳以定點或科學記數表示的數值字串。
+
{{jsxref("Number.prototype.toSource()")}} {{non-standard_inline}}
+
Returns an object literal representing the specified {{jsxref("Number")}} object; you can use this value to create a new object. Overrides the {{jsxref("Object.prototype.toSource()")}} method.
+
{{jsxref("Number.prototype.toString()")}}
+
回傳以特定基數表示的數值字串。這覆寫 {{jsxref("Object.prototype.toString()")}} 的方法 。
+
{{jsxref("Number.prototype.valueOf()")}}
+
回傳這個物件的原始型別,即原始數值。這覆寫 {{jsxref("Object.prototype.valueOf()")}} 。
+
+ +

規範

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
規範狀態註記
{{SpecName('ES1')}}{{Spec2('ES1')}}Initial definition. Implemented in JavaScript 1.1.
{{SpecName('ES5.1', '#sec-15.7.4', 'Number')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-properties-of-the-number-prototype-object', 'Number')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-properties-of-the-number-prototype-object', 'Number')}}{{Spec2('ESDraft')}} 
+ +

瀏覽器相容性

+ + + +

{{Compat("javascript.builtins.Number.prototype")}}

+ +

參見

+ + diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/toexponential/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/toexponential/index.html new file mode 100644 index 0000000000..622314ecfb --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/toexponential/index.html @@ -0,0 +1,164 @@ +--- +title: Number.prototype.toExponential() +slug: Web/JavaScript/Reference/Global_Objects/Number/toExponential +tags: + - 數字 + - 科學技數法 +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toExponential +--- +
{{JSRef}}
+ +

toExponential() method 用來將數字轉成「科學記數法」格式。

+ +

語法

+ +
numObj.toExponential([fractionDigits])
+ +

參數

+ + + + + + + + + + + + + + + + + + + + +
參數可選默認值類型說明
fractionDigits默認盡可能將數字完整顯示{{jsxref("Number")}}(正整數)小數點後的位數。需為 0 至 20 之間。
+ +

回傳直

+ +

一 string,將數字以「科學計數法」格式表示出來

+ +

Exceptions

+ +
+
{{jsxref("RangeError")}}
+
若 fractionDigits 超出範圍(可接受的範圍是 0 ~ 20 之間的正整數)觸發 {{jsxref("RangeError")}}。
+
{{jsxref("TypeError")}}
+
若參數 fractionDigits 不是 {{jsxref("Number")}},則觸發{{jsxref("TypeError")}}。
+
+ +

Description

+ +

If the fractionDigits argument is omitted, the number of digits after the decimal point defaults to the number of digits necessary to represent the value uniquely.

+ +

If you use the toExponential() method for a numeric literal and the numeric literal has no exponent and no decimal point, leave whitespace(s) before the dot that precedes the method call to prevent the dot from being interpreted as a decimal point.

+ +

If a number has more digits than requested by the fractionDigits parameter, the number is rounded to the nearest number represented by fractionDigits digits. See the discussion of rounding in the description of the {{jsxref("Number.prototype.toFixed", "toFixed()")}} method, which also applies to toExponential().

+ +

範例

+ +

Using toExponential

+ +
var numObj = 77.1234;
+
+console.log(numObj.toExponential());  // logs 7.71234e+1
+console.log(numObj.toExponential(4)); // logs 7.7123e+1
+console.log(numObj.toExponential(2)); // logs 7.71e+1
+console.log(77.1234.toExponential()); // logs 7.71234e+1
+console.log(77 .toExponential());     // logs 7.7e+1
+
+ +

Specifications

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES3')}}{{Spec2('ES3')}}Initial definition. Implemented in JavaScript 1.5.
{{SpecName('ES5.1', '#sec-15.7.4.6', 'Number.prototype.toExponential')}}{{Spec2('ES5.1')}} 
{{SpecName('ES6', '#sec-number.prototype.toexponential', 'Number.prototype.toExponential')}}{{Spec2('ES6')}} 
{{SpecName('ESDraft', '#sec-number.prototype.toexponential', 'Number.prototype.toExponential')}}{{Spec2('ESDraft')}} 
+ +

瀏覽器支援度

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}
+
+ +

延伸閱讀

+ + diff --git a/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html b/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html new file mode 100644 index 0000000000..7a5afb608f --- /dev/null +++ b/files/zh-tw/web/javascript/reference/global_objects/number/tofixed/index.html @@ -0,0 +1,108 @@ +--- +title: Number.prototype.toFixed() +slug: Web/JavaScript/Reference/Global_Objects/Number/toFixed +tags: + - JavaScript + - Method + - Number + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toFixed +--- +
{{JSRef}}
+ +

toFixed() 方法會使用定點小數表示法(fixed-point notation)來格式化數字。

+ +
{{EmbedInteractiveExample("pages/js/number-tofixed.html")}}
+ + + +

語法

+ +
numObj.toFixed([digits])
+ +

參數

+ +
+
digits 小數位
+
選擇性輸入。顯示數值至多少個小數點,範圍由0到20之間,執行時或可支援非常大範圍的數值。如果無輸入會默認做0。
+
+ +

回傳值

+ +

一個代表以定點小數表示法(fixed-point notation)格式化數字後的字串。

+ +

例外

+ +
+
{{jsxref("RangeError")}}
+
If digits is too small or too large. Values between 0 and 100, inclusive, will not cause a {{jsxref("RangeError")}}. Implementations are allowed to support larger and smaller values as chosen.
+
{{jsxref("TypeError")}}
+
If this method is invoked on an object that is not a {{jsxref( "Number")}}.
+
+ +

說明

+ +

toFixed() returns a string representation of numObj that does not use exponential notation and has exactly digits digits after the decimal place. The number is rounded if necessary, and the fractional part is padded with zeros if necessary so that it has the specified length. If numObj is greater than 1e+21, this method simply calls {{jsxref("Number.prototype.toString()")}} and returns a string in exponential notation.

+ +

範例

+ +

Using toFixed

+ +
var numObj = 12345.6789;
+
+numObj.toFixed();       // Returns '12346': note rounding, no fractional part
+numObj.toFixed(1);      // Returns '12345.7': note rounding
+numObj.toFixed(6);      // Returns '12345.678900': note added zeros
+(1.23e+20).toFixed(2);  // Returns '123000000000000000000.00'
+(1.23e-10).toFixed(2);  // Returns '0.00'
+2.34.toFixed(1);        // Returns '2.3'
+2.35.toFixed(1);        // Returns '2.4'. Note that it rounds up in this case.
+-2.34.toFixed(1);       // Returns -2.3 (due to operator precedence, negative number literals don't return a string...)
+(-2.34).toFixed(1);     // Returns '-2.3' (...unless you use parentheses)
+
+ +

規範

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('ES3')}}{{Spec2('ES3')}}Initial definition. Implemented in JavaScript 1.5.
{{SpecName('ES5.1', '#sec-15.7.4.5', 'Number.prototype.toFixed')}}{{Spec2('ES5.1')}}
{{SpecName('ES6', '#sec-number.prototype.tofixed', 'Number.prototype.toFixed')}}{{Spec2('ES6')}}
{{SpecName('ESDraft', '#sec-number.prototype.tofixed', 'Number.prototype.toFixed')}}{{Spec2('ESDraft')}}
+ +

瀏覽器相容性

+ + + +

{{Compat("javascript.builtins.Number.toFixed")}}

+ +

參見

+ + -- cgit v1.2.3-54-g00ecf