Skip to content

Commit

Permalink
Merge pull request #52406 from allgandalf/revert-50783-fix/49929-unif…
Browse files Browse the repository at this point in the history
…y-distance-rates

Revert "fix: unify distance rates display"

(cherry picked from commit 967e8f2)

(CP triggered by puneetlath)
  • Loading branch information
puneetlath authored and OSBotify committed Nov 12, 2024
1 parent 853923c commit 34e6ed4
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 16 deletions.
1 change: 0 additions & 1 deletion src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5840,7 +5840,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,
Expand Down
3 changes: 1 addition & 2 deletions src/libs/CurrencyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

Expand Down
11 changes: 6 additions & 5 deletions src/libs/PolicyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down
11 changes: 3 additions & 8 deletions tests/unit/PolicyUtilsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});
});
});
Expand Down

0 comments on commit 34e6ed4

Please sign in to comment.