@@ -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