-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(common): helpers to deal with bigint
- Loading branch information
1 parent
d46d28d
commit 7cc0320
Showing
6 changed files
with
138 additions
and
20 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
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,21 @@ | ||
import {bigintFormatter} from './bigint-formatter' | ||
|
||
describe('bigintFormatter', () => { | ||
// NOTE: decimal and thousands separators are locale-dependent | ||
it('it works', () => { | ||
let value = BigInt(1_234_567_890) | ||
let decimalPlaces = 2 | ||
let formattedValue = bigintFormatter({value, decimalPlaces}) | ||
expect(formattedValue).toBe('12,345,678.90') | ||
|
||
value = BigInt(189) | ||
decimalPlaces = 6 | ||
formattedValue = bigintFormatter({value, decimalPlaces}) | ||
expect(formattedValue).toBe('0.000189') | ||
|
||
value = BigInt(1_000_000) | ||
decimalPlaces = 6 | ||
formattedValue = bigintFormatter({value, decimalPlaces}) | ||
expect(formattedValue).toBe('1.000000') | ||
}) | ||
}) |
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 @@ | ||
import {splitBigInt} from './split-bigint' | ||
|
||
export function bigintFormatter({ | ||
value, | ||
decimalPlaces, | ||
}: { | ||
value: bigint | ||
decimalPlaces: number | ||
}) { | ||
return splitBigInt(value, decimalPlaces).bn.toFormat(decimalPlaces) | ||
} |
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
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 |
---|---|---|
@@ -1,17 +1,31 @@ | ||
import BigNumber from 'bignumber.js' | ||
import {freeze} from 'immer' | ||
|
||
export function splitBigInt( | ||
bigInt: bigint, | ||
decimalPlaces: number, | ||
): {int: bigint; dec: bigint} { | ||
export function splitBigInt(bigInt: bigint, decimalPlaces: number) { | ||
const scale: bigint = BigInt(10) ** BigInt(decimalPlaces) | ||
const integerPart: bigint = bigInt / scale | ||
const decimalPart: bigint = bigInt % scale | ||
|
||
const isNegative = bigInt < 0 | ||
const sign = isNegative ? '-' : '' | ||
const signAdjust = isNegative ? -1n : 1n | ||
|
||
const absoluteBigInt = signAdjust * bigInt | ||
const integer = (absoluteBigInt / scale).toString() | ||
const fraction = (absoluteBigInt % scale) | ||
.toString() | ||
.padStart(decimalPlaces, '0') | ||
|
||
const str = `${sign}${integer}.${fraction}` | ||
|
||
const bn = new BigNumber(str) | ||
|
||
return freeze({ | ||
int: integerPart, | ||
dec: decimalPart, | ||
bn: new BigNumber(`${integerPart}.${decimalPart}`), | ||
decimalPlaces, | ||
|
||
bi: bigInt, | ||
integer, | ||
fraction, | ||
|
||
bn, | ||
str, | ||
}) | ||
} |
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