Skip to content

Commit

Permalink
fix: formatUSD helper (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
dohaki authored Nov 24, 2023
1 parent 9cad29a commit 07142ab
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/utils/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,6 @@ export function humanReadableNumber(num: number, decimals = 0): string {
* @note USD only has 2 decimal places of precision, so this will round to the nearest cent.
*/
export function formatUSD(value: BigNumberish): string {
const formattedString = formatUnits(value, 18);
return numeral(formattedString).format("0,0.00");
const formattedString = ethers.utils.formatUnits(value, 18);
return numeral(Number(formattedString).toFixed(2)).format("0,0.00");
}
17 changes: 17 additions & 0 deletions src/utils/tests/format.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { utils } from "ethers";

import {
isValidString,
shortenAddress,
shortenString,
shortenTransactionHash,
smallNumberFormatter,
formatUSD,
} from "../format";
const VALID_ADDRESS = "0x04Fa0d235C4abf4BcF4787aF4CF447DE572eF828";
const TX_HASH =
Expand Down Expand Up @@ -55,3 +58,17 @@ describe("#smallNumberFormatter", () => {
expect(smallNumberFormatter(0.01235)).toEqual("0.0124");
});
});

describe("#formatUSD", () => {
it("should format a wei BigNumber to 2 decimal places", () => {
expect(formatUSD(utils.parseEther("1.1"))).toEqual("1.10");
});

it("should format a very small wei number to 2 decimal places", () => {
expect(formatUSD(1)).toEqual("0.00");
});

it("should format a large wei BigNumber to 2 decimal places", () => {
expect(formatUSD(utils.parseEther("100000.123456"))).toEqual("100,000.12");
});
});

2 comments on commit 07142ab

@vercel
Copy link

@vercel vercel bot commented on 07142ab Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

goerli-frontend-v2 – ./

goerli-frontend-v2-git-master-uma.vercel.app
goerli-frontend-v2-uma.vercel.app
goerli-frontend-v2.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 07142ab Nov 24, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.