From a4e5db50679892f65fd916c34caa001857639101 Mon Sep 17 00:00:00 2001 From: Michael Geers Date: Thu, 13 Aug 2026 18:05:09 +0200 Subject: [PATCH] UI: fix auto power unit for negative values (#32813) --- assets/js/mixins/formatter.test.ts | 5 +++++ assets/js/mixins/formatter.ts | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/assets/js/mixins/formatter.test.ts b/assets/js/mixins/formatter.test.ts index a2c389e32..aa240b026 100644 --- a/assets/js/mixins/formatter.test.ts +++ b/assets/js/mixins/formatter.test.ts @@ -30,6 +30,11 @@ describe("fmtW", () => { expect(fmt.fmtW(0, POWER_UNIT.W)).eq("0 W"); expect(fmt.fmtW(1200000, POWER_UNIT.W)).eq("1.200.000 W"); }); + test("should format negative values", () => { + expect(fmt.fmtW(-5300, POWER_UNIT.AUTO)).eq("-5,3 kW"); + expect(fmt.fmtW(-500, POWER_UNIT.AUTO)).eq("-500 W"); + expect(fmt.fmtW(-12000000, POWER_UNIT.AUTO)).eq("-12,0 MW"); + }); test("should format without units", () => { expect(fmt.fmtW(0, POWER_UNIT.AUTO, false)).eq("0,0"); expect(fmt.fmtW(1200000, POWER_UNIT.AUTO, false)).eq("1.200,0"); diff --git a/assets/js/mixins/formatter.ts b/assets/js/mixins/formatter.ts index de7e11538..894bd065f 100644 --- a/assets/js/mixins/formatter.ts +++ b/assets/js/mixins/formatter.ts @@ -74,7 +74,8 @@ export default defineComponent({ return (Math.round(num * base) / base).toFixed(precision); }, getPowerUnit(watt: number): POWER_UNIT { - return watt >= 10_000_000 ? POWER_UNIT.MW : watt >= 1000 ? POWER_UNIT.KW : POWER_UNIT.W; + const abs = Math.abs(watt); + return abs >= 10_000_000 ? POWER_UNIT.MW : abs >= 1000 ? POWER_UNIT.KW : POWER_UNIT.W; }, fmtW(watt = 0, format = POWER_UNIT.KW, withUnit = true, digits?: number) { let unit = format;