Skip to content

Commit b221e43

Browse files
authored
Implement consistent decimal rounding to 2 places across app
2 parents e2856e9 + 029997a commit b221e43

3 files changed

Lines changed: 127 additions & 70 deletions

File tree

create.php

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,21 @@ function calculateKcal($protein, $carbs, $fat)
9292
file_put_contents($mealsFile, json_encode($meals, JSON_PRETTY_PRINT));
9393

9494
if ($isAjax) {
95+
// Round values for JSON response
96+
$newMealRounded = [
97+
'name' => $newMeal['name'],
98+
'protein' => round($newMeal['protein'], 2),
99+
'carbs' => round($newMeal['carbs'], 2),
100+
'fat' => round($newMeal['fat'], 2),
101+
'color' => $newMeal['color'],
102+
'description' => $newMeal['description']
103+
];
95104
header('Content-Type: application/json');
96105
echo json_encode([
97106
'success' => true,
98107
'message' => 'Meal added successfully!',
99-
'meal' => $newMeal,
100-
'kcal' => calculateKcal($protein, $carbs, $fat)
108+
'meal' => $newMealRounded,
109+
'kcal' => round(calculateKcal($protein, $carbs, $fat), 2)
101110
]);
102111
exit;
103112
}
@@ -179,13 +188,22 @@ function calculateKcal($protein, $carbs, $fat)
179188
file_put_contents($mealsFile, json_encode($meals, JSON_PRETTY_PRINT));
180189

181190
if ($isAjax) {
191+
// Round values for JSON response
192+
$updatedMealRounded = [
193+
'name' => $updatedMeal['name'],
194+
'protein' => round($updatedMeal['protein'], 2),
195+
'carbs' => round($updatedMeal['carbs'], 2),
196+
'fat' => round($updatedMeal['fat'], 2),
197+
'color' => $updatedMeal['color'],
198+
'description' => $updatedMeal['description']
199+
];
182200
header('Content-Type: application/json');
183201
echo json_encode([
184202
'success' => true,
185203
'message' => 'Meal updated successfully!',
186-
'meal' => $updatedMeal,
204+
'meal' => $updatedMealRounded,
187205
'oldName' => $oldName,
188-
'kcal' => calculateKcal($protein, $carbs, $fat)
206+
'kcal' => round(calculateKcal($protein, $carbs, $fat), 2)
189207
]);
190208
exit;
191209
}
@@ -387,7 +405,7 @@ class="w-full px-3 py-2 bg-stone-50 border border-stone-200 rounded-lg text-ston
387405
</thead>
388406
<tbody>
389407
<?php foreach ($meals as $meal): ?>
390-
<?php $mealKcal = calculateKcal($meal['protein'], $meal['carbs'], $meal['fat']); ?>
408+
<?php $mealKcal = round(calculateKcal($meal['protein'], $meal['carbs'], $meal['fat']), 2); ?>
391409
<tr class="border-b border-stone-100 hover:bg-stone-50 transition-colors">
392410
<td class="py-3 px-1 text-stone-700">
393411
<div class="flex items-center justify-between gap-2">
@@ -402,9 +420,9 @@ class="info-btn p-1 text-stone-400 hover:text-stone-600 hover:bg-stone-100 round
402420
</div>
403421
</td>
404422
<td class="py-3 px-1 text-center text-stone-600 font-medium border-l border-stone-100"><?php echo $mealKcal; ?></td>
405-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo $meal['protein']; ?></td>
406-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo $meal['carbs']; ?></td>
407-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo $meal['fat']; ?></td>
423+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo round($meal['protein'], 2); ?></td>
424+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo round($meal['carbs'], 2); ?></td>
425+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo round($meal['fat'], 2); ?></td>
408426
<td class="py-3 px-1 text-center border-l border-stone-100">
409427
<div class="flex max-sm:flex-col itens-center justify-center gap-1">
410428
<button type="button"
@@ -543,14 +561,19 @@ class="w-full px-3 py-2 bg-stone-50 border border-stone-200 rounded-lg text-ston
543561
lucide.createIcons();
544562

545563
$(document).ready(function() {
564+
// Helper function to format number to max 2 decimal places
565+
function formatNumber(num) {
566+
return parseFloat(parseFloat(num).toFixed(2));
567+
}
568+
546569
// Calculate estimated KCAL for new meal form
547570
function updateEstimatedKcal() {
548571
const protein = parseFloat($('#protein').val()) || 0;
549572
const carbs = parseFloat($('#carbs').val()) || 0;
550573
const fat = parseFloat($('#fat').val()) || 0;
551574

552575
const kcal = (protein * 4) + (carbs * 4) + (fat * 9);
553-
$('#estimated-kcal').text(Math.round(kcal));
576+
$('#estimated-kcal').text(formatNumber(kcal));
554577
}
555578

556579
// Calculate estimated KCAL for edit form
@@ -560,7 +583,7 @@ function updateEditEstimatedKcal() {
560583
const fat = parseFloat($('#edit-fat').val()) || 0;
561584

562585
const kcal = (protein * 4) + (carbs * 4) + (fat * 9);
563-
$('#edit-estimated-kcal').text(Math.round(kcal));
586+
$('#edit-estimated-kcal').text(formatNumber(kcal));
564587
}
565588

566589
// Update KCAL preview on input change
@@ -685,10 +708,10 @@ function showToast(message, type) {
685708

686709
// Update row content
687710
$row.find('td:eq(0) span').first().text(response.meal.name);
688-
$row.find('td:eq(1)').text(response.kcal);
689-
$row.find('td:eq(2)').text(response.meal.protein);
690-
$row.find('td:eq(3)').text(response.meal.carbs);
691-
$row.find('td:eq(4)').text(response.meal.fat);
711+
$row.find('td:eq(1)').text(formatNumber(response.kcal));
712+
$row.find('td:eq(2)').text(formatNumber(response.meal.protein));
713+
$row.find('td:eq(3)').text(formatNumber(response.meal.carbs));
714+
$row.find('td:eq(4)').text(formatNumber(response.meal.fat));
692715

693716
// Update button data attributes
694717
const $editBtn = $row.find('.edit-meal-btn');
@@ -749,10 +772,10 @@ class="info-btn p-1 text-stone-400 hover:text-stone-600 hover:bg-stone-100 round
749772
</button>
750773
</div>
751774
</td>
752-
<td class="py-3 px-1 text-center text-stone-600 font-medium border-l border-stone-100">${kcal}</td>
753-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${meal.protein}</td>
754-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${meal.carbs}</td>
755-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${meal.fat}</td>
775+
<td class="py-3 px-1 text-center text-stone-600 font-medium border-l border-stone-100">${formatNumber(kcal)}</td>
776+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${formatNumber(meal.protein)}</td>
777+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${formatNumber(meal.carbs)}</td>
778+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${formatNumber(meal.fat)}</td>
756779
<td class="py-3 px-1 text-center border-l border-stone-100">
757780
<div class="flex max-sm:flex-col itens-center justify-center gap-1">
758781
<button type="button"

index.php

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,26 @@ function calculateKcal($protein, $carbs, $fat)
138138
$totalFat += $m['fat'];
139139
}
140140

141+
// Round values for JSON response
142+
$newMealRounded = [
143+
'name' => $newMeal['name'],
144+
'protein' => round($newMeal['protein'], 2),
145+
'carbs' => round($newMeal['carbs'], 2),
146+
'fat' => round($newMeal['fat'], 2),
147+
'color' => isset($newMeal['color']) ? $newMeal['color'] : 'blue'
148+
];
149+
141150
header('Content-Type: application/json');
142151
echo json_encode([
143152
'success' => true,
144-
'meal' => $newMeal,
145-
'mealKcal' => calculateKcal($newMeal['protein'], $newMeal['carbs'], $newMeal['fat']),
153+
'meal' => $newMealRounded,
154+
'mealKcal' => round(calculateKcal($newMeal['protein'], $newMeal['carbs'], $newMeal['fat']), 2),
146155
'mealIndex' => count($dateMeals) - 1,
147156
'totals' => [
148-
'kcal' => $totalKcal,
149-
'protein' => $totalProtein,
150-
'carbs' => $totalCarbs,
151-
'fat' => $totalFat
157+
'kcal' => round($totalKcal, 2),
158+
'protein' => round($totalProtein, 2),
159+
'carbs' => round($totalCarbs, 2),
160+
'fat' => round($totalFat, 2)
152161
]
153162
]);
154163
exit;
@@ -197,10 +206,10 @@ function calculateKcal($protein, $carbs, $fat)
197206
echo json_encode([
198207
'success' => true,
199208
'totals' => [
200-
'kcal' => $totalKcal,
201-
'protein' => $totalProtein,
202-
'carbs' => $totalCarbs,
203-
'fat' => $totalFat
209+
'kcal' => round($totalKcal, 2),
210+
'protein' => round($totalProtein, 2),
211+
'carbs' => round($totalCarbs, 2),
212+
'fat' => round($totalFat, 2)
204213
],
205214
'mealsCount' => count($dateMeals)
206215
]);
@@ -303,24 +312,24 @@ function calculateKcal($protein, $carbs, $fat)
303312
</tr>
304313
<?php else: ?>
305314
<?php foreach ($selectedDateMeals as $index => $meal): ?>
306-
<?php $mealKcal = calculateKcal($meal['protein'], $meal['carbs'], $meal['fat']); ?>
315+
<?php $mealKcal = round(calculateKcal($meal['protein'], $meal['carbs'], $meal['fat']), 2); ?>
307316
<tr class="border-b border-stone-100 hover:bg-stone-50 meal-row cursor-pointer transition-colors" data-index="<?php echo $index; ?>" data-name="<?php echo htmlspecialchars($meal['name']); ?>">
308317
<td class="py-3 px-1 text-stone-700"><?php echo htmlspecialchars($meal['name']); ?></td>
309318
<td class="py-3 px-1 text-center text-stone-600 font-medium border-l border-stone-100"><?php echo $mealKcal; ?></td>
310-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo $meal['protein']; ?></td>
311-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo $meal['carbs']; ?></td>
312-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo $meal['fat']; ?></td>
319+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo round($meal['protein'], 2); ?></td>
320+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo round($meal['carbs'], 2); ?></td>
321+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100"><?php echo round($meal['fat'], 2); ?></td>
313322
</tr>
314323
<?php endforeach; ?>
315324
<?php endif; ?>
316325

317326
<!-- Totals Row -->
318327
<tr class="bg-stone-100">
319328
<td class="py-3 px-1 font-semibold text-stone-800">Total</td>
320-
<td class="py-3 px-1 text-center font-semibold text-stone-800 border-l border-stone-100"><?php echo $totalKcal; ?></td>
321-
<td class="py-3 px-1 text-center font-medium text-stone-600 border-l border-stone-100"><?php echo $totalProtein; ?></td>
322-
<td class="py-3 px-1 text-center font-medium text-stone-600 border-l border-stone-100"><?php echo $totalCarbs; ?></td>
323-
<td class="py-3 px-1 text-center font-medium text-stone-600 border-l border-stone-100"><?php echo $totalFat; ?></td>
329+
<td class="py-3 px-1 text-center font-semibold text-stone-800 border-l border-stone-100"><?php echo round($totalKcal, 2); ?></td>
330+
<td class="py-3 px-1 text-center font-medium text-stone-600 border-l border-stone-100"><?php echo round($totalProtein, 2); ?></td>
331+
<td class="py-3 px-1 text-center font-medium text-stone-600 border-l border-stone-100"><?php echo round($totalCarbs, 2); ?></td>
332+
<td class="py-3 px-1 text-center font-medium text-stone-600 border-l border-stone-100"><?php echo round($totalFat, 2); ?></td>
324333
</tr>
325334
</tbody>
326335
</table>
@@ -336,7 +345,7 @@ function calculateKcal($protein, $carbs, $fat)
336345

337346
<div class="grid grid-cols-2 md:grid-cols-3 gap-3">
338347
<?php foreach ($meals as $meal): ?>
339-
<?php $mealKcal = calculateKcal($meal['protein'], $meal['carbs'], $meal['fat']); ?>
348+
<?php $mealKcal = round(calculateKcal($meal['protein'], $meal['carbs'], $meal['fat']), 2); ?>
340349
<div class="relative">
341350
<form method="post" class="meal-form h-full">
342351
<input type="hidden" name="meal_name" value="<?php echo htmlspecialchars($meal['name']); ?>">
@@ -496,10 +505,10 @@ class="w-full px-4 py-3 border border-stone-300 rounded-lg text-stone-700 focus:
496505
const newRow = `
497506
<tr class="border-b border-stone-100 hover:bg-stone-50 meal-row cursor-pointer transition-colors" data-index="${response.mealIndex}" data-name="${escapeHtml(response.meal.name)}" style="opacity: 0;">
498507
<td class="py-3 px-1 text-stone-700">${escapeHtml(response.meal.name)}</td>
499-
<td class="py-3 px-1 text-center text-stone-600 font-medium border-l border-stone-100">${response.mealKcal}</td>
500-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${response.meal.protein}</td>
501-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${response.meal.carbs}</td>
502-
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${response.meal.fat}</td>
508+
<td class="py-3 px-1 text-center text-stone-600 font-medium border-l border-stone-100">${formatNumber(response.mealKcal)}</td>
509+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${formatNumber(response.meal.protein)}</td>
510+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${formatNumber(response.meal.carbs)}</td>
511+
<td class="py-3 px-1 text-center text-stone-500 border-l border-stone-100">${formatNumber(response.meal.fat)}</td>
503512
</tr>
504513
`;
505514

@@ -583,10 +592,10 @@ function bindMealRowEvents() {
583592
// Function to update totals
584593
function updateTotals(totals) {
585594
const $totalsRow = $('tbody tr.bg-stone-100');
586-
$totalsRow.find('td:eq(1)').text(totals.kcal);
587-
$totalsRow.find('td:eq(2)').text(totals.protein);
588-
$totalsRow.find('td:eq(3)').text(totals.carbs);
589-
$totalsRow.find('td:eq(4)').text(totals.fat);
595+
$totalsRow.find('td:eq(1)').text(formatNumber(totals.kcal));
596+
$totalsRow.find('td:eq(2)').text(formatNumber(totals.protein));
597+
$totalsRow.find('td:eq(3)').text(formatNumber(totals.carbs));
598+
$totalsRow.find('td:eq(4)').text(formatNumber(totals.fat));
590599
}
591600

592601
// Function to re-index meal rows after deletion
@@ -604,6 +613,11 @@ function escapeHtml(text) {
604613
return div.innerHTML;
605614
}
606615

616+
// Helper function to format number to max 2 decimal places
617+
function formatNumber(num) {
618+
return parseFloat(parseFloat(num).toFixed(2));
619+
}
620+
607621
// Info button click (show description)
608622
$('.info-btn').on('click', function(e) {
609623
e.stopPropagation();

0 commit comments

Comments
 (0)