https://github.com/jellydn/ethers-bignumber-demo
How to convert to BigNumber's ethers.js without error.
https://github.com/jellydn/ethers-bignumber-demo
bignumber ethersjs web3
Last synced: about 2 months ago
JSON representation
How to convert to BigNumber's ethers.js without error.
- Host: GitHub
- URL: https://github.com/jellydn/ethers-bignumber-demo
- Owner: jellydn
- Created: 2023-02-25T02:44:07.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-15T14:41:05.000Z (over 2 years ago)
- Last Synced: 2025-03-11T21:54:15.379Z (over 1 year ago)
- Topics: bignumber, ethersjs, web3
- Language: TypeScript
- Homepage: https://dash.deno.com/playground/ether-big-number
- Size: 9.77 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
Welcome to ethers-bignumber-demo 👋
How to convert to BigNumber's ethers.js without error.
[![IT Man - Tech #34 - How to use BigNumber's ethers.js like PRO [Vietnamese]](https://i.ytimg.com/vi/uzE813GIeY4/hqdefault.jpg)](https://www.youtube.com/watch?v=uzE813GIeY4)
## Motivation
You might have these kind of errors when use `BigNumber.from()` function.
```typescript
BigNumber.from(1.23);
```
```bash
Error: underflow [ See: https://links.ethers.org/v5-errors-NUMERIC_FAULT-underflow ] (fault="underflow", operation="BigNumber.from", value=1.23, code=NUMERIC_FAULT, version=bignumber/5.7.0)
```
or
```typescript
BigNumber.from(Number.MAX_SAFE_INTEGER);
```
```bash
Error: overflow [ See: https://links.ethers.org/v5-errors-NUMERIC_FAULT-overflow ] (fault="overflow", operation="BigNumber.from", value=9007199254740991, code=NUMERIC_FAULT, version=bignumber/5.7.0)
```
## Solution
```typescript
import { formatUnits, parseUnits } from "npm:@ethersproject/units";
import { BigNumber, BigNumberish } from "npm:@ethersproject/bignumber";
export function convertToCurrencyBigNumber(val: string | number): BigNumber {
return parseUnits(Number(val).toFixed(2), 2);
}
export function convertFromCurrencyBigNumber(val: BigNumberish): string {
return formatUnits(val, 2);
}
```
## Usage
```typescript
convertToCurrencyBigNumber(300.59999999999997); // BigNumber { _hex: "0x756c", _isBigNumber: true }
convertFromCurrencyBigNumber(convertToCurrencyBigNumber(300.59999999999997)); // 300.6
```
## Develop
You need to install [deno](https://deno.land/manual@v1.31.0/getting_started/installation) before run below command.
```sh
deno task dev
```
## Test
```sh
deno test
```
## Reference
- [How to pass unsafe numbers into BigNumber? · Issue #452 · ethers-io/ethers.js](https://github.com/ethers-io/ethers.js/issues/452)
## Author
👤 **Huynh Duc Dung**
- Website: https://productsway.com/
- Twitter: [@jellydn](https://twitter.com/jellydn)
- Github: [@jellydn](https://github.com/jellydn)
## Show your support
Give a ⭐️ if this project helped you!
[](https://ko-fi.com/Q5Q61Q7YM)