Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix precision error when creating distance request #28734

Merged
merged 2 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/libs/DistanceRequestUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ const getDistanceMerchant = (hasRoute, distanceInMeters, unit, rate, currency, t
* @param {Number} distance - The distance traveled in meters
* @param {'mi' | 'km'} unit - The unit of measurement for the distance
* @param {Number} rate - Rate used for calculating the request amount
* @returns {Number} The computed request amount.
* @returns {Number} The computed request amount in "cents" (integer).
aldo-expensify marked this conversation as resolved.
Show resolved Hide resolved
*/
const getDistanceRequestAmount = (distance, unit, rate) => {
const convertedDistance = convertDistanceUnit(distance, unit);
const roundedDistance = convertedDistance.toFixed(2);
return roundedDistance * rate;
return Math.round(roundedDistance * rate);
};

export default {getDefaultMileageRate, getDistanceMerchant, getDistanceRequestAmount};
12 changes: 12 additions & 0 deletions tests/unit/DistanceRequestUtilsTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import DistanceRequestUtils from '../../src/libs/DistanceRequestUtils';

describe('DistanceRequestUtils', () => {
describe('getDistanceRequestAmount', () => {
test.each([
[350, 8605.146, 'mi', 65.5],
[561, 8605.146, 'km', 65.1],
])('Correctly calculates amount %s for %s%s at a rate of %s per unit', (expectedResult, distance, unit, rate) => {
expect(DistanceRequestUtils.getDistanceRequestAmount(distance, unit, rate)).toBe(expectedResult);
});
});
});
Loading