Skip to content

Commit

Permalink
fix(tests): Do not accrue floating point errors in money calculation …
Browse files Browse the repository at this point in the history
…for business-to-consumer-tax-calculation-gross-without-checkpoints-test (#618)
  • Loading branch information
lukehesluke authored Aug 6, 2024
1 parent 633625b commit 7024b00
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { FlowStageUtils } = require('./flow-stage-utils');
const { OrderFeedUpdateFlowStageUtils } = require('./order-feed-update');
const { PFlowStage } = require('./p');
const { TestInterfaceActionFlowStage } = require('./test-interface-action');
const { fixCalculatedMoneyValue } = require('../money-utils');

/**
* @typedef {import('../logger').BaseLoggerType} BaseLoggerType
Expand Down Expand Up @@ -297,7 +298,10 @@ const FlowStageRecipes = {
}
result.totalPaymentDue += price;
}
return result;
return {
...result,
totalPaymentDue: fixCalculatedMoneyValue(result.totalPaymentDue),
};
})();
return {
orderItems: fetchOpportunities.getOutput().orderItems,
Expand Down
24 changes: 24 additions & 0 deletions packages/openactive-integration-tests/test/helpers/money-utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* If performing calculations on money values, use this function on the final
* result to ensure that it does not accrue floating point errors.
*
* e.g.
*
* ```
* > 55.2 * 3
* 165.60000000000002
* > fixCalculatedMoneyValue(55.2 * 3)
* 165.6
* ```
*
* @param {number} valuePounds
* @returns {number}
*/
function fixCalculatedMoneyValue(valuePounds) {
const valuePence = valuePounds * 100;
return Math.round(valuePence) / 100;
}

module.exports = {
fixCalculatedMoneyValue,
};

0 comments on commit 7024b00

Please sign in to comment.