Skip to content

Commit

Permalink
Merge pull request #50783 from thelullabyy/fix/49929-unify-distance-r…
Browse files Browse the repository at this point in the history
…ates

fix: unify distance rates display
  • Loading branch information
puneetlath authored Nov 8, 2024
2 parents 0474d28 + fb3fa5b commit a7b1553
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/CONST.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/libs/CurrencyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
}

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

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

0 comments on commit a7b1553

Please sign in to comment.