--- title: BigInt.prototype.toString() slug: Web/JavaScript/Reference/Global_Objects/BigInt/toString tags: - BigInt - JavaScript - Method - Prototype - toString() translation_of: Web/JavaScript/Reference/Global_Objects/BigInt/toString --- <div>{{JSRef}}</div> <p><strong><code>toString()</code></strong>メソッドは、指定した {{jsxref("BigInt")}} オブジェクトを表す文字列を返します。末尾の "n" は返り値の文字列の一部にはなりません。</p> <div>{{EmbedInteractiveExample("pages/js/bigint-tostring.html")}}</div> <p class="hidden">このデモのソースファイルは GitHub リポジトリに格納されています。デモプロジェクトに協力したい場合は、 <a href="https://github.com/mdn/interactive-examples">https://github.com/mdn/interactive-examples</a> をクローンしてプルリクエストを送信してください。</p> <h2 id="Syntax" name="Syntax">構文</h2> <pre class="syntaxbox notranslate"><code><var>bigIntObj</var>.toString([<var>radix</var>])</code></pre> <h3 id="Parameters" name="Parameters">パラメーター</h3> <dl> <dt><code>radix</code>{{optional_inline}}</dt> <dd>オプション。数値を表す底に指定する 2 以上 36 以下の整数(基数)。</dd> </dl> <h3 id="Return_value" name="Return_value">戻り値</h3> <p>指定した {{jsxref("BigInt")}} オブジェクトを表す文字列。</p> <h3 id="Exceptions" name="Exceptions">例外</h3> <dl> <dt>{{jsxref("RangeError")}}</dt> <dd><code>toString()</code> が 2 未満または 36 より大きな基数を与えられた場合、{{jsxref("RangeError")}} がスローされます。</dd> </dl> <h2 id="Description" name="Description">説明</h2> <p>{{jsxref("BigInt")}} オブジェクトは {{jsxref("Object")}} の <code>toString()</code> メソッドをオーバーライドします。つまり、{{jsxref("Object.prototype.toString()")}} を継承しません。{{jsxref( "BigInt")}} オブジェクトでは、<code>toString()</code> メソッドは指定された基数によるオブジェクトの文字列表現を返します。</p> <p><code>toString()</code> メソッドは 1 番目の引数を解析し、指定された基数(底)による文字列表現を返そうとします。10 より大きな基数が指定された場合は、アルファベットが 9 より大きな数値を示します。例えば、16 進数(底が 16)では <code>a</code> から <code>f</code> が使用されます。</p> <p><code>radix</code> が指定されなかった場合は、基数として 10 が指定されたとみなします。</p> <p><code>bigIntObj</code> が負の数だった場合、符号は保持されます。これは、基数が 2 だった場合にも適用されます。つまり、返り値の文字列は、<code>bigIntObj</code> の正の 2 進数表現の先頭に <code>-</code> 符号が付いたものであり、<code>bigIntObj</code> の 2 の補数<strong>ではありません。</strong></p> <h2 id="Examples" name="Examples">例</h2> <h3 id="Using_toString" name="Using_toString"><code>toString</code>の使用</h3> <pre class="brush: js notranslate">17n.toString(); // '17' 66n.toString(2); // '1000010' 254n.toString(16); // 'fe' -10n.toString(2); // -1010' -0xffn.toString(2); // '-11111111' </pre> <h3 id="Negative-zero_BigInt" name="Negative-zero_BigInt"><code>BigInt</code> における負の 0</h3> <p>整数に負の 0 が存在しないのと同様に、<code>BigInt</code> には負の 0 がありません。<code>-0.0</code> は JavaScript の {{jsxref("Number")}} 型にのみ現れる、IEEE 浮動小数点数の概念です。</p> <pre class="brush: js notranslate">(-0n).toString(); // '0' BigInt(-0).toString(); // '0'</pre> <h2 id="Specifications" name="Specifications">仕様</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">仕様書</th> </tr> <tr> <td>{{SpecName('ESDraft', '#sec-bigint.prototype.tostring', 'BigInt.prototype.toString()')}}</td> </tr> </tbody> </table> <h2 id="Browser_compatibility" name="Browser_compatibility">ブラウザー実装状況</h2> <p>{{Compat("javascript.builtins.BigInt.toString")}}</p> <h2 id="See_also" name="See_also">関連情報</h2> <ul> <li>{{jsxref("BigInt.prototype.toLocaleString()")}}</li> <li>{{jsxref("BigInt.prototype.valueOf()")}}</li> <li>{{jsxref("Number.prototype.toString()")}}</li> </ul>