Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support toBigInt method #356

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions bignumber.d.ts
Expand Up @@ -1423,6 +1423,19 @@ export declare class BigNumber implements BigNumber.Instance {
*/
toNumber(): number;

/**
* Returns the value of this BigNumber as a JavaScript primitive BigInt.
*
* ```ts
* x = new BigNumber(456)
* x.toBigInt() // 456n
*
* y = new BigNumber('45987349857634085409857349856430985')
* y.toBigInt() // 45987349857634085409857349856430985n
* ```
*/
toBigInt(): BigInt;

/**
* Returns a string representing the value of this BigNumber rounded to `significantDigits`
* significant digits using rounding mode `roundingMode`.
Expand Down
10 changes: 10 additions & 0 deletions bignumber.js
Expand Up @@ -42,6 +42,7 @@
* toFraction |
* toJSON |
* toNumber |
* toBigInt |
* toPrecision |
* toString |
* valueOf |
Expand Down Expand Up @@ -2694,6 +2695,15 @@
return +valueOf(this);
};

/*
* Return the value of this BigNumber converted to a BigInt primitive.
*/
P.toBigInt = function () {
if (!this.isInteger()) throw Error(bignumberError + 'Not an integer: ' + valueOf(this));
var str = coeffToString(this.c);
for (var i = this.e + 1 - str.length; i > 0; i--) str += '0';
return BigInt(this.s < 0 ? '-' + str : str);
};

/*
* Return a string representing the value of this BigNumber rounded to sd significant digits
Expand Down
11 changes: 11 additions & 0 deletions bignumber.mjs
Expand Up @@ -39,6 +39,7 @@
* toFraction |
* toJSON |
* toNumber |
* toBigInt |
* toPrecision |
* toString |
* valueOf |
Expand Down Expand Up @@ -2669,6 +2670,16 @@ function clone(configObject) {
return +valueOf(this);
};

/*
* Return the value of this BigNumber converted to a BigInt primitive.
*/
P.toBigInt = function () {
if (!this.isInteger()) throw Error(bignumberError + 'Not an integer: ' + valueOf(this));
var str = coeffToString(this.c);
for (var i = this.e + 1 - str.length; i > 0; i--) str += '0';
return BigInt(this.s < 0 ? '-' + str : str);
};


/*
* Return a string representing the value of this BigNumber rounded to sd significant digits
Expand Down
11 changes: 11 additions & 0 deletions doc/API.html
Expand Up @@ -141,6 +141,7 @@
<li><a href="#toFr" >toFraction </a> </li>
<li><a href="#toJSON" >toJSON </a> </li>
<li><a href="#toN" >toNumber </a> </li>
<li><a href="#toB" >toBigInt </a> </li>
<li><a href="#toP" >toPrecision </a> </li>
<li><a href="#toS" >toString </a> </li>
<li><a href="#valueOf">valueOf </a> </li>
Expand Down Expand Up @@ -1761,6 +1762,16 @@ <h5 id="toN">toNumber<code class='inset'>.toNumber() <i>&rArr; number</i></code>
1 / z.toNumber() // -Infinity
1 / +z // -Infinity</pre>

<h5 id="toB">toBigInt<code class='inset'>.toBigInt() <i>&rArr; BigInt</i></code></h5>
<p>Returns the value of this BigNumber as a JavaScript BigInt primitive.</p>
<pre>
x = new BigNumber(456)
x.toBigInt() // 456n

y = new BigNumber('45987349857634085409857349856430985')
y.toBigInt() // 45987349857634085409857349856430985n
</pre>



<h5 id="toP">
Expand Down
46 changes: 46 additions & 0 deletions test/methods/toBigInt.js
@@ -0,0 +1,46 @@
if (typeof Test === 'undefined') require('../tester');

Test('toBigInt', function () {

function t(value, n) {
Test.areEqual(n, new BigNumber(value).toBigInt());
}

function tx(fn, msg){
Test.isException(fn, msg);
}

t(0, 0n);
t(-0, 0n);
t(1, 1n);
t(1, 1n);
t(-1, -1n);
t('-1', -1n);
t('1.0', 1n);
t('-1.0', -1n);
t('11111.0000', 11111n);
t('-11111.0000', -11111n);

t('1234567890000000', 1234567890000000n);
t('1234567890000000', 1234567890000000n);
t('-1234567890000000', -1234567890000000n);
t('1e18', BigInt(1e18));
t('-1e18', BigInt(-1e18));

// large integers
t('1e50', BigInt(`1${'0'.repeat(50)}`));
t('-1e50', BigInt(`-1${'0'.repeat(50)}`));
t('1e100', BigInt(`1${'0'.repeat(100)}`));

// non-integers
tx(() => new BigNumber(null).toBigInt(), '(null)');
tx(() => new BigNumber(NaN).toBigInt(), '(NaN)');
tx(() => new BigNumber(Infinity).toBigInt(), '(Infinity)');
tx(() => new BigNumber(-Infinity).toBigInt(), '(-Infinity)');
tx(() => new BigNumber('123abcdefg').toBigInt(), '(123abcdefg)');
tx(() => new BigNumber('abcdefg').toBigInt(), '(abcdefg)');
tx(() => new BigNumber(1.1).toBigInt(), '(1.1)');
tx(() => new BigNumber(-1.1).toBigInt(), '(-1.1)');
tx(() => new BigNumber(123123123.123).toBigInt(), '(123123123.123)');
tx(() => new BigNumber(321.123).toBigInt(), '(321.123)');
});
1 change: 1 addition & 0 deletions test/test.html
Expand Up @@ -43,6 +43,7 @@
'toFormat',
'toFraction',
'toNumber',
'toBigInt',
'toPrecision',
'toString'
];
Expand Down
1 change: 1 addition & 0 deletions test/test.js
Expand Up @@ -33,6 +33,7 @@ console.log('\n Testing bignumber.js\n');
'toFormat',
'toFraction',
'toNumber',
'toBigInt',
'toPrecision',
'toString'
]
Expand Down