Handle NaN values in total cost calculations

This commit is contained in:
2024-12-15 22:16:49 +08:00
parent f257d9e5b3
commit 3cd6bc5bc2

View File

@@ -1,6 +1,9 @@
import { STORAGE_NAME_TOTALCOST } from "@/const"; import { STORAGE_NAME_TOTALCOST } from "@/const";
export function addTotalCost(cost: number) { export function addTotalCost(cost: number) {
if (isNaN(cost)) {
return;
}
let totalCost = getTotalCost(); let totalCost = getTotalCost();
totalCost += cost; totalCost += cost;
localStorage.setItem(STORAGE_NAME_TOTALCOST, `${totalCost}`); localStorage.setItem(STORAGE_NAME_TOTALCOST, `${totalCost}`);
@@ -10,6 +13,9 @@ export function getTotalCost(): number {
let totalCost = parseFloat( let totalCost = parseFloat(
localStorage.getItem(STORAGE_NAME_TOTALCOST) ?? "0", localStorage.getItem(STORAGE_NAME_TOTALCOST) ?? "0",
); );
if (isNaN(totalCost)) {
totalCost = 0;
}
return totalCost; return totalCost;
} }