Fixed number precision in locations page spool card

This commit is contained in:
Donkie
2024-11-25 19:07:24 +01:00
parent c57141254c
commit 04920e463a
2 changed files with 4 additions and 2 deletions

View File

@@ -10,6 +10,7 @@ import utc from "dayjs/plugin/utc";
import { useEffect, useRef } from "react"; import { useEffect, useRef } from "react";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import SpoolIcon from "../../../components/spoolIcon"; import SpoolIcon from "../../../components/spoolIcon";
import { formatWeight } from "../../../utils/parsing";
import { ISpool } from "../../spools/model"; import { ISpool } from "../../spools/model";
import { ItemTypes, SpoolDragItem, useCurrentDraggedSpool } from "../dnd"; import { ItemTypes, SpoolDragItem, useCurrentDraggedSpool } from "../dnd";
@@ -145,7 +146,7 @@ export function SpoolCard({
if (spool.filament.material) str += spool.filament.material + " - "; if (spool.filament.material) str += spool.filament.material + " - ";
if (spool.filament.weight) { if (spool.filament.weight) {
const remaining_weight = spool.remaining_weight ?? 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) { if (spool.last_used) {
// Format like "last used X time ago" // Format like "last used X time ago"

View File

@@ -97,7 +97,8 @@ export function formatWeight(weightInGrams: number, precision: number = 2): stri
const kilograms = (weightInGrams / 1000).toFixed(precision).replace(/\.?0+$/, ""); // Remove trailing zeros const kilograms = (weightInGrams / 1000).toFixed(precision).replace(/\.?0+$/, ""); // Remove trailing zeros
return `${kilograms} kg`; return `${kilograms} kg`;
} else { } else {
return `${weightInGrams} g`; const grams = weightInGrams.toFixed(precision).replace(/\.?0+$/, ""); // Remove trailing zeros
return `${grams} g`;
} }
} }