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)}`)