From 9bc538fd7c89d88627433a7bd8e6312a3f1d0866 Mon Sep 17 00:00:00 2001 From: Alexander Wassbjer <52541059+alexanderwassbjer@users.noreply.github.com> Date: Sun, 27 Oct 2024 20:50:57 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20bug=20with=20floating=20nu?= =?UTF-8?q?mbers=20on=20exact=20split=20calculator=20(#135)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix bug with floating numbers on exact split calculator --- .../AddExpense/SplitTypeSection.tsx | 33 ++++++++----------- src/store/addStore.ts | 13 +++++--- 2 files changed, 21 insertions(+), 25 deletions(-) diff --git a/src/components/AddExpense/SplitTypeSection.tsx b/src/components/AddExpense/SplitTypeSection.tsx index bdcb200..8478d4b 100644 --- a/src/components/AddExpense/SplitTypeSection.tsx +++ b/src/components/AddExpense/SplitTypeSection.tsx @@ -137,27 +137,20 @@ const SplitEqualSection: React.FC = () => { const allSelected = participants.every((p) => p.splitShare !== 0); return ( -
{currency}
- ({ ...p, amount: p.splitShare ?? 0 })); - canSplitScreenClosed = - amount - participants.reduce((acc, p) => acc + (p.splitShare ?? 0), 0) === 0; - break; + case SplitType.EXACT: + const totalSplitShare = participants.reduce((acc, p) => acc + (p.splitShare ?? 0), 0); + + const epsilon = 0.01; + canSplitScreenClosed = Math.abs(amount - totalSplitShare) < epsilon; + + updatedParticipants = participants.map((p) => ({ ...p, amount: p.splitShare ?? 0 })); + break; case SplitType.ADJUSTMENT: const totalAdjustment = participants.reduce((acc, p) => acc + (p.splitShare ?? 0), 0); if (totalAdjustment > amount) {