Skip to content

Commit

Permalink
fix(APP-3417): Fix number formats to truncate small numbers (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
cgero-eth authored Aug 20, 2024
1 parent 82c8e42 commit 477506f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 15 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

- Update `<Wallet />` module component to correctly propagate custom `chainId` and `wagmi` configs to
`<MemberAvatar />` component
- Fix `FIAT_TOTAL_SHORT`, `FIAT_TOTAL_LONG`, `TOKEN_AMOUNT_SHORT` and `PERCENTAGE_SHORT` formats to truncate small
numbers

### Changed

Expand Down
22 changes: 11 additions & 11 deletions src/core/utils/formatterUtils/formatterUtils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ describe('formatter utils', () => {
{ value: -1234.56789, result: '-$1,234.57' },
{ value: -1234.56789, result: '-1.234,57 USD', locale: 'it' },
{ value: -0.012345678, result: '-$0.01' },
{ value: -0.0012345678, result: '-$0.001' },
{ value: -0.0012345678, result: '-$0.00' },
{ value: 0, result: '$0.00' },
{ value: 0.0012345678, result: '$0.001' },
{ value: 0.0012345678, result: '+$0.001', withSign: true },
{ value: 0.0012345678, result: '$0.00' },
{ value: 0.0012345678, result: '+$0.00', withSign: true },
{ value: 0.012345678, result: '$0.01' },
{ value: 0.012345678, result: '+$0.01', withSign: true },
{ value: 0.12345678, result: '$0.12' },
Expand All @@ -97,10 +97,10 @@ describe('formatter utils', () => {

test.each([
{ value: -1234.56789, result: '-$1.23K' },
{ value: -0.0012345678, result: '-$0.001' },
{ value: -0.0012345678, result: '-$0.00' },
{ value: 0, result: '$0.00' },
{ value: 0.0012345678, result: '$0.001' },
{ value: 0.0012345678, result: '+$0.001', withSign: true },
{ value: 0.0012345678, result: '$0.00' },
{ value: 0.0012345678, result: '+$0.00', withSign: true },
{ value: 0.012345678, result: '$0.01' },
{ value: 0.12345678, result: '$0.12' },
{ value: 123.45678, result: '$123.46' },
Expand Down Expand Up @@ -145,9 +145,9 @@ describe('formatter utils', () => {
test.each([
{ value: -1234, result: '-1.23K' },
{ value: -1234, result: '-1,23K', locale: 'it' },
{ value: -0.0012, result: '-0.001' },
{ value: -0.0012, result: '-0' },
{ value: 0, result: '0' },
{ value: 0.0012, result: '0.001' },
{ value: 0.0012, result: '0' },
{ value: 0.0123456789012345678, result: '0.01' },
{ value: 0.12345678901234567, result: '0.12' },
{ value: 123.4567, result: '123.46' },
Expand Down Expand Up @@ -216,10 +216,10 @@ describe('formatter utils', () => {
{ value: -0.999001, result: '-99.9%' },
{ value: -0.999001, result: '-99,9%', locale: 'it' },
{ value: -0.12345, result: '-12.3%' },
{ value: -0.00012345, result: '-0.01%' },
{ value: -0.00012345, result: '-0%' },
{ value: 0, result: '0%' },
{ value: 0.00012345, result: '0.01%' },
{ value: 0.00012345, result: '+0.01%', withSign: true },
{ value: 0.00012345, result: '0%' },
{ value: 0.00012345, result: '+0%', withSign: true },
{ value: 0.0012345, result: '0.1%' },
{ value: 0.012345, result: '1.2%' },
{ value: 0.12345, result: '12.3%' },
Expand Down
4 changes: 0 additions & 4 deletions src/core/utils/formatterUtils/formatterUtilsDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,19 +71,16 @@ export const numberFormats: Record<NumberFormat, INumberFormat> = {
},
[NumberFormat.FIAT_TOTAL_SHORT]: {
fixedFractionDigits: 2,
maxSignificantDigits: (value) => (Math.abs(value) < 0.01 ? 1 : undefined),
useBaseSymbol: true,
isCurrency: true,
},
[NumberFormat.FIAT_TOTAL_LONG]: {
fixedFractionDigits: 2,
maxSignificantDigits: (value) => (Math.abs(value) < 0.01 ? 1 : undefined),
isCurrency: true,
},
[NumberFormat.TOKEN_AMOUNT_SHORT]: {
maxFractionDigits: 2,
useBaseSymbol: true,
maxSignificantDigits: (value) => (Math.abs(value) < 0.01 ? 1 : undefined),
},
[NumberFormat.TOKEN_AMOUNT_LONG]: {
maxFractionDigits: 18,
Expand All @@ -97,7 +94,6 @@ export const numberFormats: Record<NumberFormat, INumberFormat> = {
},
[NumberFormat.PERCENTAGE_SHORT]: {
maxFractionDigits: 1,
maxSignificantDigits: (value) => (Math.abs(value) < 0.1 ? 1 : undefined),
isPercentage: true,
},
[NumberFormat.PERCENTAGE_LONG]: {
Expand Down

0 comments on commit 477506f

Please sign in to comment.