diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index f6f9da4ce685..bd59e849e4c9 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.MIN_TAX_RATE_DECIMAL_PLACES); + return numValue; } /** @@ -151,11 +151,10 @@ function getRateDisplayValue(value: number, toLocaleDigit: (arg: string) => stri } if (withDecimals) { - const decimalPart = numValue.toString().split('.').at(1); - if (decimalPart) { - const fractionDigits = Math.min(decimalPart.length, CONST.MAX_TAX_RATE_DECIMAL_PLACES); - return Number(numValue).toFixed(fractionDigits).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'); }); }); });