From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- .../global_objects/number/toprecision/index.html | 92 ++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 files/ja/web/javascript/reference/global_objects/number/toprecision/index.html (limited to 'files/ja/web/javascript/reference/global_objects/number/toprecision/index.html') diff --git a/files/ja/web/javascript/reference/global_objects/number/toprecision/index.html b/files/ja/web/javascript/reference/global_objects/number/toprecision/index.html new file mode 100644 index 0000000000..61e5aae3d2 --- /dev/null +++ b/files/ja/web/javascript/reference/global_objects/number/toprecision/index.html @@ -0,0 +1,92 @@ +--- +title: Number.prototype.toPrecision() +slug: Web/JavaScript/Reference/Global_Objects/Number/toPrecision +tags: + - JavaScript + - Method + - Number + - Prototype +translation_of: Web/JavaScript/Reference/Global_Objects/Number/toPrecision +--- +
{{JSRef}}
+ +

toPrecision() メソッドは {{jsxref("Number")}} オブジェクトを指定された精度で表した文字列を返します。

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

構文

+ +
numObj.toPrecision([precision])
+ +

引数

+ +
+
precision {{optional_inline}}
+
有効数字の数を指定する整数です。
+
+ +

返値

+ +

{{jsxref("Number")}} オブジェクトを precision で指定された桁で概数化された、固定小数点数、または指数表記で表した文字列です。概数の表現方法については {{jsxref("Number.prototype.toFixed()")}} メソッドの説明を参照してください。それは toPrecision() にも適用されます。

+ +

引数 precision が省略された場合、 {{jsxref("Number.prototype.toString()")}} のように振舞います。 precision が整数の値ではない場合は、最も近い整数に概数化されます。

+ +

例外

+ +
+
{{jsxref("Global_Objects/RangeError", "RangeError")}}
+
precision が 1 と 100 の間 (両端を含む) でない場合、 {{jsxref("RangeError")}} が発生します。実装上はこの範囲を超えた値にも対応できます。 ECMA-262 では 21 桁までの精度のみを要求しています。
+
+ +

+ +

toPrecision の使用

+ +
let numObj = 5.123456
+
+console.log(numObj.toPrecision())    // logs '5.123456'
+console.log(numObj.toPrecision(5))   // logs '5.1235'
+console.log(numObj.toPrecision(2))   // logs '5.1'
+console.log(numObj.toPrecision(1))   // logs '5'
+
+numObj = 0.000123
+
+console.log(numObj.toPrecision())    // logs '0.000123'
+console.log(numObj.toPrecision(5))   // logs '0.00012300'
+console.log(numObj.toPrecision(2))   // logs '0.00012'
+console.log(numObj.toPrecision(1))   // logs '0.0001'
+
+// note that exponential notation might be returned in some circumstances
+console.log((1234.5).toPrecision(2)) // logs '1.2e+3'
+
+ +

仕様書

+ + + + + + + + + + + + +
仕様書
{{SpecName('ESDraft', '#sec-number.prototype.toprecision', 'Number.prototype.toPrecision')}}
+ +

ブラウザーの互換性

+ + + +

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

+ +

関連情報

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