fix(labs): keep three significant digits for a lab value below one

This commit is contained in:
Marc-André Bombeck committed 2026-10-14 19:57:34 +02:00
1 parent 784ea4ca9d
commit 38ef4c7fa7
2 files changed
+17 -6

No files matched your search

+8 -1
View File
@@ -10,10 +10,17 @@ describe("lab value formatting", () => {
it("trims to two decimals and prints the locale's separator", () => {
expect(formatLabValue(1.8, de)).toBe("1,8");
expect(formatLabValue(1.8, en)).toBe("1.8");
expect(formatLabValue(0.123456, de)).toBe("0,12");
expect(formatLabValue(0.123456, de)).toBe("0,123");
expect(formatLabValue(12.3456, de)).toBe("12,35");
expect(formatLabValue(5, de)).toBe("5");
});
it("never shows a small nonzero reading as zero", () => {
expect(formatLabValue(0.004, de)).toBe("0,004");
expect(formatLabValue(0.012, en)).toBe("0.012");
expect(formatLabValue(0.035, de)).toBe("0,035");
});
it("formats a numeric reading with its unit and leaves a qualitative one verbatim", () => {
expect(
formatLabReading({ value: 61.3, valueText: null, unit: "kg" }, de),
+9 -5
View File
@@ -3,8 +3,9 @@
*
* The list card, the detail history rows, and the chart tooltip all trim a
* lab reading the same way: whole numbers render bare, fractional values keep
* up to two decimals (no trailing-zero noise). Centralised so the three
* surfaces stay byte-identical.
* up to two decimals (no trailing-zero noise), and a value below 1 keeps three
* significant digits so a small reading (TSH 0.004, troponin 0.012) never
* reads as zero. Centralised so the three surfaces stay byte-identical.
*
* The digits themselves come from the caller's locale number formatter
* (`useFormatters().number`), so a German reader sees "1,8 mIU/L" where an
@@ -16,9 +17,12 @@ export function formatLabValue(
value: number,
formatNumber: LabNumberFormat,
): string {
return formatNumber(
Number.isInteger(value) ? value : Number(value.toFixed(2)),
);
if (Number.isInteger(value)) return formatNumber(value);
const rounded =
Math.abs(value) < 1
? Number(value.toPrecision(3))
: Number(value.toFixed(2));
return formatNumber(rounded);
}
/**