From ab720272daa2f816bb11bc91034fbefaa5f8b6f4 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GENO133\\IvanPlex" Date: Fri, 19 Jan 2024 00:00:13 -0700 Subject: [PATCH] fix divide by zero. --- Helper/GasHelper.cs | 9 ++++++--- wwwroot/js/gasrecord.js | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/Helper/GasHelper.cs b/Helper/GasHelper.cs index b3a9a44..752d4eb 100644 --- a/Helper/GasHelper.cs +++ b/Helper/GasHelper.cs @@ -42,7 +42,7 @@ namespace CarCareTracker.Helper Gallons = convertedConsumption, Cost = currentObject.Cost, DeltaMileage = deltaMileage, - CostPerGallon = currentObject.Cost / convertedConsumption, + CostPerGallon = convertedConsumption > 0.00M ? currentObject.Cost / convertedConsumption : 0, IsFillToFull = currentObject.IsFillToFull, MissedFuelUp = currentObject.MissedFuelUp }; @@ -57,7 +57,10 @@ namespace CarCareTracker.Helper else if (currentObject.IsFillToFull) { //if user filled to full. - gasRecordViewModel.MilesPerGallon = useMPG ? (unFactoredMileage + deltaMileage) / (unFactoredConsumption + convertedConsumption) : 100 / ((unFactoredMileage + deltaMileage) / (unFactoredConsumption + convertedConsumption)); + if (convertedConsumption > 0.00M) + { + gasRecordViewModel.MilesPerGallon = useMPG ? (unFactoredMileage + deltaMileage) / (unFactoredConsumption + convertedConsumption) : 100 / ((unFactoredMileage + deltaMileage) / (unFactoredConsumption + convertedConsumption)); + } //reset unFactored vars unFactoredConsumption = 0; unFactoredMileage = 0; @@ -83,7 +86,7 @@ namespace CarCareTracker.Helper Cost = currentObject.Cost, DeltaMileage = 0, MilesPerGallon = 0, - CostPerGallon = currentObject.Cost / convertedConsumption, + CostPerGallon = convertedConsumption > 0.00M ? currentObject.Cost / convertedConsumption : 0, IsFillToFull = currentObject.IsFillToFull, MissedFuelUp = currentObject.MissedFuelUp }); diff --git a/wwwroot/js/gasrecord.js b/wwwroot/js/gasrecord.js index 13d7534..414b324 100644 --- a/wwwroot/js/gasrecord.js +++ b/wwwroot/js/gasrecord.js @@ -89,7 +89,7 @@ function getAndValidateGasRecordValues() { } else { $("#gasRecordMileage").removeClass("is-invalid"); } - if (gasGallons.trim() == '' || parseInt(gasGallons) < 0) { + if (gasGallons.trim() == '' || parseFloat(gasGallons) <= 0) { hasError = true; $("#gasRecordGallons").addClass("is-invalid"); } else {