UI: fix auto power unit for negative values (#32813)

This commit is contained in:
Michael Geers 2026-08-13 18:05:09 +02:00 • committed by GitHub
parent 0e58436ca4
commit a4e5db5067
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 7 additions and 1 deletions

View file

@ -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");

View file

@ -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;