diff --git a/src/CONST.ts b/src/CONST.ts index 07bb20741c74..e95e3d4a5603 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -5840,6 +5840,7 @@ const CONST = { MAX_TAX_RATE_INTEGER_PLACES: 4, MAX_TAX_RATE_DECIMAL_PLACES: 4, + MIN_TAX_RATE_DECIMAL_PLACES: 2, DOWNLOADS_PATH: '/Downloads', DOWNLOADS_TIMEOUT: 5000, diff --git a/src/libs/CurrencyUtils.ts b/src/libs/CurrencyUtils.ts index f9ac681cb468..b701a32a7c98 100644 --- a/src/libs/CurrencyUtils.ts +++ b/src/libs/CurrencyUtils.ts @@ -164,7 +164,8 @@ function convertAmountToDisplayString(amount = 0, currency: string = CONST.CURRE return NumberFormatUtils.format(BaseLocaleListener.getPreferredLocale(), convertedAmount, { style: 'currency', currency, - minimumFractionDigits: CONST.MAX_TAX_RATE_DECIMAL_PLACES, + minimumFractionDigits: CONST.MIN_TAX_RATE_DECIMAL_PLACES, + maximumFractionDigits: CONST.MAX_TAX_RATE_DECIMAL_PLACES, }); } diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index 3f7b6af9f951..4688a11d18dc 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -126,7 +126,7 @@ function getNumericValue(value: number | string, toLocaleDigit: (arg: string) => if (Number.isNaN(numValue)) { return NaN; } - return numValue.toFixed(CONST.CUSTOM_UNITS.RATE_DECIMALS); + return numValue; } /** @@ -158,11 +158,10 @@ function getRateDisplayValue(value: number, toLocaleDigit: (arg: string) => stri } if (withDecimals) { - const decimalPart = numValue.toString().split('.').at(1); - if (decimalPart) { - const fixedDecimalPoints = decimalPart.length > 2 && !decimalPart.endsWith('0') ? 3 : 2; - return Number(numValue).toFixed(fixedDecimalPoints).toString().replace('.', toLocaleDigit('.')); - } + const decimalPart = numValue.toString().split('.').at(1) ?? ''; + // Set the fraction digits to be between 2 and 4 (OD Behavior) + const fractionDigits = Math.min(Math.max(decimalPart.length, CONST.MIN_TAX_RATE_DECIMAL_PLACES), CONST.MAX_TAX_RATE_DECIMAL_PLACES); + return Number(numValue).toFixed(fractionDigits).toString().replace('.', toLocaleDigit('.')); } return numValue.toString().replace('.', toLocaleDigit('.')).substring(0, value.toString().length); diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index 8178bb99e877..e760bb2040c7 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -34,14 +34,19 @@ describe('PolicyUtils', () => { expect(rate).toEqual('10.50'); }); + it('should return non-integer value with 4 decimals as is', () => { + const rate = PolicyUtils.getRateDisplayValue(10.5312, toLocaleDigitMock, true); + expect(rate).toEqual('10.5312'); + }); + it('should return non-integer value with 3 decimals as is', () => { const rate = PolicyUtils.getRateDisplayValue(10.531, toLocaleDigitMock, true); expect(rate).toEqual('10.531'); }); - it('should return non-integer value with 3+ decimals cut to 3', () => { - const rate = PolicyUtils.getRateDisplayValue(10.531345, toLocaleDigitMock, true); - expect(rate).toEqual('10.531'); + it('should return non-integer value with 4+ decimals cut to 4', () => { + const rate = PolicyUtils.getRateDisplayValue(10.531255, toLocaleDigitMock, true); + expect(rate).toEqual('10.5313'); }); }); });