diff --git a/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.js b/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.js index 418033b0f..83de8eced 100644 --- a/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.js +++ b/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.js @@ -177,11 +177,12 @@ export default class CommonSelectLandParcelPageController extends LandGrantsQues const landParcelMetadata = selectedParcelIds.map((id) => { const parcel = parcelMap.get(id) const rawArea = parcel?.area?.value - return { parcelId: id, areaHa: rawArea == null ? null : Number(rawArea) } + const areaHa = rawArea == null ? null : (Number(rawArea) * 10000) / 10000 + return { parcelId: id, areaHa } }) const totalHectaresForSelectedParcels = - Math.round(landParcelMetadata.reduce((sum, { areaHa }) => sum + (areaHa ?? 0), 0) * 10000) / 10000 + (landParcelMetadata.reduce((sum, { areaHa }) => sum + (areaHa ?? 0), 0) * 10000) / 10000 if (this.minimumAreaHa !== null && totalHectaresForSelectedParcels < this.minimumAreaHa) { const parcels = mapParcelsToViewModel(fetchedParcels) diff --git a/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.test.js b/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.test.js index ed7c66912..8fa233bd8 100644 --- a/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.test.js +++ b/src/server/land-grants/common/common-select-parcel/common-select-land-parcel-page.controller.test.js @@ -244,7 +244,7 @@ describe('CommonSelectLandParcelPageController', () => { expect(controller.setState).not.toHaveBeenCalled() }) - it('rounds totalHectaresForSelectedParcels to 4dp to avoid float precision issues', async () => { + it('passes parcel area sum to GAS', async () => { fetchParcels.mockResolvedValue([ { sheetId: 'S1', parcelId: 'P1', area: { value: 25.3874 } }, { sheetId: 'S2', parcelId: 'P2', area: { value: 169.8586 } } @@ -260,12 +260,13 @@ describe('CommonSelectLandParcelPageController', () => { await controller.handlePost(request, context, h) + const expectedTotal = 25.3874 + 169.8586 expect(controller.mergeState).toHaveBeenCalledWith( request, context.state, expect.objectContaining({ - totalHectaresForSelectedParcels: 195.246, - additionalAnswers: { totalHectaresForSelectedParcels: 195.246 } + totalHectaresForSelectedParcels: expectedTotal, + additionalAnswers: { totalHectaresForSelectedParcels: expectedTotal } }) ) }) diff --git a/src/server/woodland/woodland-hectares-page.controller.js b/src/server/woodland/woodland-hectares-page.controller.js index e3492d833..1aa536da3 100644 --- a/src/server/woodland/woodland-hectares-page.controller.js +++ b/src/server/woodland/woodland-hectares-page.controller.js @@ -13,10 +13,9 @@ const HECTARES_OVER_TEN_FIELD_NAME = 'hectaresTenOrOverYearsOld' const HECTARES_UNDER_TEN_FIELD_NAME = 'hectaresUnderTenYearsOld' const ERROR_BELOW_MINIMUM = `The total area of woodland must be at least ${MIN_WOODLAND_TOTAL_AREA_HA}ha` -const truncate4dp = (/** @type {number} */ n) => Math.floor(n * 10000) / 10000 const errorExceedsMax = (/** @type {number} */ max) => - `Total area of woodland cannot be more than total area of selected land parcels (${truncate4dp(max)}ha)` + `Total area of woodland cannot be more than total area of selected land parcels (${max}ha)` /** * @param {string} fieldName @@ -53,7 +52,7 @@ export default class WoodlandHectaresPageController extends TaskPageController { getViewModel(request, context) { const { state } = context - const totalHectaresForSelectedParcels = truncate4dp(Number(state['totalHectaresForSelectedParcels'] ?? 0)) + const totalHectaresForSelectedParcels = Number(state['totalHectaresForSelectedParcels'] ?? 0) const viewModel = /** @type {Record} */ (super.getViewModel(request, context)) const guidanceIndex = viewModel.components?.findIndex(