diff --git a/packages/mobile/src/utils/formatting.test.ts b/packages/mobile/src/utils/formatting.test.ts index 9c23df01c9e..37186a16e8e 100644 --- a/packages/mobile/src/utils/formatting.test.ts +++ b/packages/mobile/src/utils/formatting.test.ts @@ -9,8 +9,8 @@ import { describe('utils->formatting', () => { describe('getMoneyDisplayValue', () => { - const UNROUNDED_NUMBER = 5.239895 - const ROUNDED_NUMBER_2_DECIMALS = '5.23' + const UNROUNDED_NUMBER = 5.239835 + const ROUNDED_NUMBER_2_DECIMALS = '5.24' const ROUNDED_NUMBER_3_DECIMALS = '5.239' it('formats correctly for default case', () => { diff --git a/packages/mobile/src/utils/formatting.ts b/packages/mobile/src/utils/formatting.ts index 5db448e7002..1fd85c0955c 100644 --- a/packages/mobile/src/utils/formatting.ts +++ b/packages/mobile/src/utils/formatting.ts @@ -9,11 +9,12 @@ const numeral = require('numeral') export const getMoneyDisplayValue = ( value: BigNumber.Value, currency: CURRENCY_ENUM = CURRENCY_ENUM.DOLLAR, - includeSymbol: boolean = false + includeSymbol: boolean = false, + roundingTolerance: number = 1 ): string => { const decimals = CURRENCIES[currency].displayDecimals const symbol = CURRENCIES[currency].symbol - const formattedValue = numeral(roundDown(value, decimals).toNumber()).format( + const formattedValue = numeral(roundDown(value, decimals, roundingTolerance).toNumber()).format( '0,0.' + '0'.repeat(decimals) ) return includeSymbol ? symbol + formattedValue : formattedValue @@ -57,11 +58,31 @@ export const divideByWei = (value: BigNumber.Value, decimals?: number) => { return decimals ? bn.decimalPlaces(decimals) : bn } -export function roundDown(value: BigNumber.Value, decimals: number = 2): BigNumber { +export function roundDown( + value: BigNumber.Value, + decimals: number = 2, + roundingTolerance: number = 0 +): BigNumber { + if (roundingTolerance) { + value = new BigNumber(value).decimalPlaces( + decimals + roundingTolerance, + BigNumber.ROUND_HALF_DOWN + ) + } return new BigNumber(value).decimalPlaces(decimals, BigNumber.ROUND_DOWN) } -export function roundUp(value: BigNumber.Value, decimals: number = 2): BigNumber { +export function roundUp( + value: BigNumber.Value, + decimals: number = 2, + roundingTolerance: number = 0 +): BigNumber { + if (roundingTolerance) { + value = new BigNumber(value).decimalPlaces( + decimals + roundingTolerance, + BigNumber.ROUND_HALF_DOWN + ) + } return new BigNumber(value).decimalPlaces(decimals, BigNumber.ROUND_UP) }