From 04920e463a690099a1efb3064a396f84d5c9d183 Mon Sep 17 00:00:00 2001 From: Donkie Date: Mon, 25 Nov 2024 19:07:24 +0100 Subject: [PATCH] Fixed number precision in locations page spool card --- client/src/pages/locations/components/spoolCard.tsx | 3 ++- client/src/utils/parsing.tsx | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/client/src/pages/locations/components/spoolCard.tsx b/client/src/pages/locations/components/spoolCard.tsx index 8191226..1dbeaaf 100644 --- a/client/src/pages/locations/components/spoolCard.tsx +++ b/client/src/pages/locations/components/spoolCard.tsx @@ -10,6 +10,7 @@ import utc from "dayjs/plugin/utc"; import { useEffect, useRef } from "react"; import { Link } from "react-router-dom"; import SpoolIcon from "../../../components/spoolIcon"; +import { formatWeight } from "../../../utils/parsing"; import { ISpool } from "../../spools/model"; import { ItemTypes, SpoolDragItem, useCurrentDraggedSpool } from "../dnd"; @@ -145,7 +146,7 @@ export function SpoolCard({ if (spool.filament.material) str += spool.filament.material + " - "; if (spool.filament.weight) { const remaining_weight = spool.remaining_weight ?? spool.filament.weight; - str += `${remaining_weight} / ${spool.filament.weight} g`; + str += `${formatWeight(remaining_weight, 0)} / ${formatWeight(spool.filament.weight, 0)}`; } if (spool.last_used) { // Format like "last used X time ago" diff --git a/client/src/utils/parsing.tsx b/client/src/utils/parsing.tsx index 8e1dff6..fc18d6f 100644 --- a/client/src/utils/parsing.tsx +++ b/client/src/utils/parsing.tsx @@ -97,7 +97,8 @@ export function formatWeight(weightInGrams: number, precision: number = 2): stri const kilograms = (weightInGrams / 1000).toFixed(precision).replace(/\.?0+$/, ""); // Remove trailing zeros return `${kilograms} kg`; } else { - return `${weightInGrams} g`; + const grams = weightInGrams.toFixed(precision).replace(/\.?0+$/, ""); // Remove trailing zeros + return `${grams} g`; } }