-
Notifications
You must be signed in to change notification settings - Fork 516
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add examples for BigInt API * Address review comments * Correct expected output comment
- Loading branch information
Showing
6 changed files
with
97 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<pre> | ||
<code id="static-js">const max = 2n ** (64n - 1n) - 1n; | ||
|
||
function check64bit(number) { | ||
(number > max) ? | ||
console.log("Number doesn't fit in signed 64-bit integer!") : | ||
console.log(BigInt.asIntN(64, number)); | ||
} | ||
|
||
check64bit(2n ** 64n); | ||
// expected output: "Number doesn't fit in signed 64-bit integer!" | ||
|
||
check64bit(2n ** 32n); | ||
// expected output: 4294967296n | ||
</code> | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<pre> | ||
<code id="static-js">const max = 2n ** 64n - 1n; | ||
|
||
function check64bit(number) { | ||
(number > max) ? | ||
console.log("Number doesn't fit in unsigned 64-bit integer!") : | ||
console.log(BigInt.asUintN(64, number)); | ||
} | ||
|
||
check64bit(2n ** 64n); | ||
// expected output: "Number doesn't fit in unsigned 64-bit integer!" | ||
|
||
check64bit(2n ** 32n); | ||
// expected output: 4294967296n | ||
</code> | ||
</pre> |
12 changes: 12 additions & 0 deletions
12
live-examples/js-examples/bigint/bigint-tolocalestring.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<pre> | ||
<code id="static-js">const bigint = 123456789123456789n; | ||
|
||
// German uses period for thousands | ||
console.log(bigint.toLocaleString('de-DE')); | ||
// expected output: "123.456.789.123.456.789" | ||
|
||
// request a currency format | ||
console.log(bigint.toLocaleString('de-DE', { style: 'currency', currency: 'EUR' })); | ||
// expected output: "123.456.789.123.456.789,00 €" | ||
</code> | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<pre> | ||
<code id="static-js">console.log(1024n.toString()); | ||
// expected output: "1024" | ||
|
||
console.log(1024n.toString(2)); | ||
// expected output: "10000000000" | ||
|
||
console.log(1024n.toString(16)); | ||
// expected output: "400" | ||
</code> | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<pre> | ||
<code id="static-js">console.log(typeof Object(1n)); | ||
// expected output: "object" | ||
|
||
console.log(typeof Object(1n).valueOf()); | ||
// expected output: "bigint" | ||
</code> | ||
</pre> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"pages": { | ||
"bigIntasIntN": { | ||
"exampleCode": "./live-examples/js-examples/bigint/bigint-asintn.html", | ||
"fileName": "bigint-asintn.html", | ||
"title": "JavaScript Demo: BigInt.asIntN()", | ||
"type": "js" | ||
}, | ||
"bigIntasUintN": { | ||
"exampleCode": "./live-examples/js-examples/bigint/bigint-asuintn.html", | ||
"fileName": "bigint-asuintn.html", | ||
"title": "JavaScript Demo: BigInt.asUintN()", | ||
"type": "js" | ||
}, | ||
"bigIntToLocaleString": { | ||
"exampleCode": "./live-examples/js-examples/bigint/bigint-tolocalestring.html", | ||
"fileName": "bigint-tolocalestring.html", | ||
"title": "JavaScript Demo: BigInt.toLocaleString()", | ||
"type": "js" | ||
}, | ||
"bigIntToString": { | ||
"exampleCode": "./live-examples/js-examples/bigint/bigint-tostring.html", | ||
"fileName": "bigint-tostring.html", | ||
"title": "JavaScript Demo: BigInt.toString()", | ||
"type": "js" | ||
}, | ||
"bigIntValueof": { | ||
"exampleCode": "./live-examples/js-examples/bigint/bigint-valueof.html", | ||
"fileName": "bigint-valueof.html", | ||
"title": "JavaScript Demo: BigInt.valueOf()", | ||
"type": "js" | ||
} | ||
} | ||
} |