Skip to content

Commit

Permalink
fix edge case formatNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
rinchan01 committed Nov 27, 2024
1 parent 0e32d92 commit 268f009
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ export function tryParseBigInt(value: string | number): bigint | null {
}

export function formatNumber(value: bigint, decimals: number): string {
if (value === 0n) {
return '0';
}
const numberString = value.toString();

if (numberString.length <= decimals) {
return `0.${numberString.padStart(decimals, '0')}`;
}
Expand Down
1 change: 0 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,5 @@ describe('Utils functions tests', () => {
expect(formatNumber(BigInt(123), 5)).toEqual('0.00123');
expect(formatNumber(123000n, 2)).toEqual("1230");
expect(formatNumber(BigInt(0), 5)).toEqual('0');

})
})

0 comments on commit 268f009

Please sign in to comment.