Skip to content

Commit 9bc538f

Browse files
🐛 Fix bug with floating numbers on exact split calculator (#135)
Fix bug with floating numbers on exact split calculator
1 parent 464cc4f commit 9bc538f

File tree

2 files changed

+21
-25
lines changed

2 files changed

+21
-25
lines changed

src/components/AddExpense/SplitTypeSection.tsx

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -137,27 +137,20 @@ const SplitEqualSection: React.FC = () => {
137137
const allSelected = participants.every((p) => p.splitShare !== 0);
138138

139139
return (
140-
<div className="mt-4 flex flex-col gap-6 px-2 relative">
140+
<div className="relative mt-4 flex flex-col gap-6 px-2">
141141
<div className="flex items-center">
142-
<div className="mb-2 flex-grow flex justify-center">
143-
<div
144-
className={`${canSplitScreenClosed ? 'text-gray-300' : 'text-red-500'
145-
}`}
146-
>
142+
<div className="mb-2 flex flex-grow justify-center">
143+
<div className={`${canSplitScreenClosed ? 'text-gray-300' : 'text-red-500'}`}>
147144
{currency} {(amount / totalParticipants).toFixed(2)} per person
148145
</div>
149146
</div>
150147
</div>
151-
<div className="absolute top-0 right-0">
148+
<div className="absolute right-0 top-0">
152149
<button
153-
className="flex items-center gap-1 border rounded-md py-0.5 px-2 whitespace-nowrap"
150+
className="flex items-center gap-1 whitespace-nowrap rounded-md border px-2 py-0.5"
154151
onClick={selectAll}
155152
>
156-
{allSelected ? (
157-
<X className="h-4 w-4" />
158-
) : (
159-
<Check className="h-4 w-4" />
160-
)}
153+
{allSelected ? <X className="h-4 w-4" /> : <Check className="h-4 w-4" />}
161154
<span className="text-sm">All</span>
162155
</button>
163156
</div>
@@ -243,7 +236,7 @@ const SplitByAmountSection: React.FC = () => {
243236
const [splitShareValue, setSplitShareValue] = useState(
244237
participants.reduce(
245238
(acc, p) => {
246-
acc[p.id] = p.splitShare?.toString();
239+
acc[p.id] = p.splitShare?.toString() ?? '';
247240
return acc;
248241
},
249242
{} as Record<string, string | undefined>,
@@ -256,19 +249,20 @@ const SplitByAmountSection: React.FC = () => {
256249
addOrUpdateParticipant({ ...p, splitShare: 0 });
257250
return;
258251
}
259-
addOrUpdateParticipant({ ...p, splitShare: parseFloat(value) });
252+
const formattedValue = parseFloat(parseFloat(value).toFixed(2));
253+
addOrUpdateParticipant({ ...p, splitShare: formattedValue });
260254
};
261255

262-
const remainingPercentage =
263-
amount - participants.reduce((acc, p) => acc + (p.splitShare ?? 0), 0);
256+
const totalSplitShare = participants.reduce((acc, p) => acc + (p.splitShare ?? 0), 0);
257+
258+
const remainingAmount = parseFloat((amount - totalSplitShare).toFixed(2));
264259

265260
return (
266261
<div className="mt-4 flex flex-col gap-6 px-2">
267262
<div
268263
className={`mb-2 text-center ${canSplitScreenClosed ? 'text-gray-300' : 'text-red-500'} t`}
269264
>
270-
{' '}
271-
Remaining {currency} {remainingPercentage}
265+
Remaining {currency} {remainingAmount}
272266
</div>
273267
{participants.map((p) => (
274268
<div key={p.id} className="flex justify-between">
@@ -278,7 +272,6 @@ const SplitByAmountSection: React.FC = () => {
278272
</div>
279273
<div className="flex items-center gap-1">
280274
<p className="text-xs">{currency}</p>
281-
282275
<Input
283276
type="number"
284277
value={splitShareValue[p.id]}

src/store/addStore.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -207,11 +207,14 @@ export function calculateParticipantSplit(
207207
amount: ((p.splitShare ?? 0) * amount) / totalShare,
208208
}));
209209
break;
210-
case SplitType.EXACT:
211-
updatedParticipants = participants.map((p) => ({ ...p, amount: p.splitShare ?? 0 }));
212-
canSplitScreenClosed =
213-
amount - participants.reduce((acc, p) => acc + (p.splitShare ?? 0), 0) === 0;
214-
break;
210+
case SplitType.EXACT:
211+
const totalSplitShare = participants.reduce((acc, p) => acc + (p.splitShare ?? 0), 0);
212+
213+
const epsilon = 0.01;
214+
canSplitScreenClosed = Math.abs(amount - totalSplitShare) < epsilon;
215+
216+
updatedParticipants = participants.map((p) => ({ ...p, amount: p.splitShare ?? 0 }));
217+
break;
215218
case SplitType.ADJUSTMENT:
216219
const totalAdjustment = participants.reduce((acc, p) => acc + (p.splitShare ?? 0), 0);
217220
if (totalAdjustment > amount) {

0 commit comments

Comments
 (0)