aboutsummaryrefslogtreecommitdiff
path: root/files/ko/web/javascript/reference/global_objects/bigint/tostring/index.html
blob: 3b04cd9f84cf3ea7fc543650b47a3c0cede06ced (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
---
title: BigInt.prototype.toString()
slug: Web/JavaScript/Reference/Global_Objects/BigInt/toString
tags:
- BigInt
- JavaScript
- Method
- Prototype
- toString()
browser-compat: javascript.builtins.BigInt.toString
---
<div>{{JSRef}}</div>

<p>The <strong><code>toString()</code></strong> method returns a string representing the
  specified {{jsxref("BigInt")}} object. The trailing "n" is not part of the string.</p>

<div>{{EmbedInteractiveExample("pages/js/bigint-tostring.html")}}</div>

<h2 id="Syntax">Syntax</h2>

<pre class="brush: js">
toString()
toString(radix)
</pre>

<h3 id="Parameters">Parameters</h3>

<dl>
  <dt><code>radix</code>{{optional_inline}}</dt>
  <dd>Optional. An integer in the range 2 through 36 specifying the base to use for
    representing numeric values.</dd>
</dl>

<h3 id="Return_value">Return value</h3>

<p>A string representing the specified {{jsxref("BigInt")}} object.</p>

<h3 id="Exceptions">Exceptions</h3>

<dl>
  <dt>{{jsxref("RangeError")}}</dt>
  <dd>If <code>toString()</code> is given a radix less than 2 or greater than 36, a
    {{jsxref("RangeError")}} is thrown.</dd>
</dl>

<h2 id="Description">Description</h2>

<p>The {{jsxref("BigInt")}} object overrides the <code>toString()</code> method of the
  {{jsxref("Object")}} object; it does not inherit
  {{jsxref("Object.prototype.toString()")}}. For {{jsxref( "BigInt")}} objects, the
  <code>toString()</code> method returns a string representation of the object in the
  specified radix.</p>

<p>The <code>toString()</code> method parses its first argument, and attempts to return a
  string representation in the specified radix (base). For radixes above 10, the letters
  of the alphabet indicate numerals greater than 9. For example, for hexadecimal numbers
  (base 16) <code>a</code> through <code>f</code> are used.</p>

<p>If the <code>radix</code> is not specified, the preferred radix is assumed to be 10.
</p>

<p>If the <code>bigIntObj</code> is negative, the sign is preserved. This is the case even
  if the radix is 2; the string returned is the positive binary representation of the
  <code>bigIntObj</code> preceded by a <code>-</code> sign, <strong>not</strong> the two's
  complement of the <code>bigIntObj</code>.</p>

<h2 id="Examples">Examples</h2>

<h3 id="Using_toString">Using <code>toString</code></h3>

<pre class="brush: js">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">Negative-zero <code>BigInt</code></h3>

<p>There is no negative-zero <code>BigInt</code> as there are no negative zeros in
  integers. <code>-0.0</code> is an IEEE floating-point concept that only appears in the
  JavaScript {{jsxref("Number")}} type.</p>

<pre class="brush: js">(-0n).toString();      // '0'
BigInt(-0).toString(); // '0'</pre>

<h2 id="Specifications">Specifications</h2>

{{Specifications}}

<h2 id="Browser_compatibility">Browser compatibility</h2>


<p>{{Compat}}</p>

<h2 id="See_also">See also</h2>

<ul>
  <li>{{jsxref("BigInt.prototype.toLocaleString()")}}</li>
  <li>{{jsxref("BigInt.prototype.valueOf()")}}</li>
  <li>{{jsxref("Number.prototype.toString()")}}</li>
</ul>