From f6e8eae9fd69e8c851c63ac7a2657aa88bda7f2a Mon Sep 17 00:00:00 2001 From: Gandalf Date: Tue, 12 Nov 2024 21:01:49 +0530 Subject: [PATCH] Revert "fix: unify distance rates display" --- src/CONST.ts | 1 - src/libs/CurrencyUtils.ts | 3 +-- src/libs/PolicyUtils.ts | 11 ++++++----- tests/unit/PolicyUtilsTest.ts | 11 +++-------- 4 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/CONST.ts b/src/CONST.ts index d93eb230f779..7f1ebb8d55d1 100755 --- a/src/CONST.ts +++ b/src/CONST.ts @@ -5830,7 +5830,6 @@ 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 b701a32a7c98..f9ac681cb468 100644 --- a/src/libs/CurrencyUtils.ts +++ b/src/libs/CurrencyUtils.ts @@ -164,8 +164,7 @@ function convertAmountToDisplayString(amount = 0, currency: string = CONST.CURRE return NumberFormatUtils.format(BaseLocaleListener.getPreferredLocale(), convertedAmount, { style: 'currency', currency, - minimumFractionDigits: CONST.MIN_TAX_RATE_DECIMAL_PLACES, - maximumFractionDigits: CONST.MAX_TAX_RATE_DECIMAL_PLACES, + minimumFractionDigits: CONST.MAX_TAX_RATE_DECIMAL_PLACES, }); } diff --git a/src/libs/PolicyUtils.ts b/src/libs/PolicyUtils.ts index f038006730af..509539421419 100644 --- a/src/libs/PolicyUtils.ts +++ b/src/libs/PolicyUtils.ts @@ -129,7 +129,7 @@ function getNumericValue(value: number | string, toLocaleDigit: (arg: string) => if (Number.isNaN(numValue)) { return NaN; } - return numValue; + return numValue.toFixed(CONST.CUSTOM_UNITS.RATE_DECIMALS); } /** @@ -161,10 +161,11 @@ function getRateDisplayValue(value: number, toLocaleDigit: (arg: string) => stri } if (withDecimals) { - 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('.')); + 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('.')); + } } return numValue.toString().replace('.', toLocaleDigit('.')).substring(0, value.toString().length); diff --git a/tests/unit/PolicyUtilsTest.ts b/tests/unit/PolicyUtilsTest.ts index e760bb2040c7..8178bb99e877 100644 --- a/tests/unit/PolicyUtilsTest.ts +++ b/tests/unit/PolicyUtilsTest.ts @@ -34,19 +34,14 @@ 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 4+ decimals cut to 4', () => { - const rate = PolicyUtils.getRateDisplayValue(10.531255, toLocaleDigitMock, true); - expect(rate).toEqual('10.5313'); + 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'); }); }); });