evm-bn
v1.1.2
Published
Convert fixed-point numbers to ethers big numbers and vice-versa
Downloads
5,270
Maintainers
Readme
Evm Bn
Convert fixed-point numbers to ethers big numbers and vice-versa. This is useful for EVM-related projects, since 1 ETH is 1e18 wei.
- Accepts scientific notation.
- Limits the precision to 78 digits.
- Enforces 60 integer digits and 18 fractional digits.
- Designed to be used alongside @ethersproject/bignumber.
- Slices the fractional digits automatically at position
n + 1
and above, withn
the number of decimals, rounding down in the process.
Install
With yarn:
$ yarn add evm-bn
Or npm:
$ npm install evm-bn
Usage
To Bn
import type { BigNumber } from "@ethersproject/bignumber";
import { toBn } from "evm-bn";
// 3141500000000000000
const foo: BigNumber = toBn("3.1415");
// 115792089237316195423570985008687907853269984665640564039457584007913129639935
const bar: BigNumber = toBn("115792089237316195423570985008687907853269984665640564039457.584007913129639935");
// 100000000000000
const baz: BigNumber = toBn("100e6", 6);
From Bn
import type { BigNumber } from "@ethersproject/bignumber";
import { fromBn } from "evm-bn";
// 3.1415
const foo: BigNumber = fromBn(BigNumber.from("3141500000000000000"));
// 115792089237316195423570985008687907853269984665640564039457.584007913129639935
const bar: BigNumber = fromBn(
BigNumber.from("115792089237316195423570985008687907853269984665640564039457584007913129639935"),
);
// 100000000
const baz: BigNumber = fromBn(BigNumber.from("100000000000000"), 6);
Contributing
Feel free to dive in! Open an issue, start a discussion or submit a PR.
Set Up
Clone the repositories and install the dependencies:
$ yarn install
Now you can start making changes.
License
MIT © Paul Razvan Berg