From be531a7ccf6a22703a160c43abeb54d3b096515f Mon Sep 17 00:00:00 2001 From: moerp Date: Tue, 9 Apr 2024 20:17:35 +0200 Subject: [PATCH] runden --- src/templates/index.html | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/templates/index.html b/src/templates/index.html index dcb27e7..1097d41 100644 --- a/src/templates/index.html +++ b/src/templates/index.html @@ -177,6 +177,17 @@ function updateRowWithNewData(row, weight, nutritionData) { } + function schulrunden(zahl) { + // Multipliziere die Zahl mit 10, um die relevante Dezimalstelle vor das Komma zu bekommen + zahl = zahl * 10; + // Wende Math.ceil an, um auf die nächste ganze Zahl aufzurunden, + // aber nur, wenn der Dezimalteil >= 0.5 ist. Andernfalls verwende Math.floor. + zahl = (zahl - Math.floor(zahl) >= 0.5) ? Math.ceil(zahl) : Math.floor(zahl); + // Teile durch 10, um die ursprüngliche Skalierung wiederherzustellen, + // aber mit der erforderlichen Rundung + return zahl / 10; + } + function addProduct() { const foodInput = document.getElementById('my_combobox'); @@ -187,7 +198,7 @@ function updateRowWithNewData(row, weight, nutritionData) { const portions = portionsInput.value ? parseInt(portionsInput.value, 10) : 1; // Teilen des Gewichts durch die Anzahl der Portionen und Aufrunden - weight = Math.ceil(weight / portions); + weight = schulrunden(Math.ceil(weight / portions)); fetch(`/add_lm?food=${encodeURIComponent(food)}&weight=${encodeURIComponent(weight)}`)