diff --git a/src/lib/labs/__tests__/format-value.test.ts b/src/lib/labs/__tests__/format-value.test.ts index 35a14fc0e..85ad43216 100644 --- a/src/lib/labs/__tests__/format-value.test.ts +++ b/src/lib/labs/__tests__/format-value.test.ts @@ -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), diff --git a/src/lib/labs/format-value.ts b/src/lib/labs/format-value.ts index 844ac0072..3626d7e4e 100644 --- a/src/lib/labs/format-value.ts +++ b/src/lib/labs/format-value.ts @@ -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); } /**