mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-27 11:16:35 +00:00
Short-format negative and small numbers in energy-distribution-card (#25862)
This commit is contained in:
parent
2dfe5f50a6
commit
e7a04eb3d2
@ -1114,12 +1114,16 @@ export const formatConsumptionShort = (
|
||||
if (!consumption) {
|
||||
return `0 ${unit}`;
|
||||
}
|
||||
const units = ["kWh", "MWh", "GWh", "TWh"];
|
||||
const units = ["Wh", "kWh", "MWh", "GWh", "TWh"];
|
||||
let pickedUnit = unit;
|
||||
let val = consumption;
|
||||
let unitIndex = units.findIndex((u) => u === unit);
|
||||
if (unitIndex >= 0) {
|
||||
while (val >= 1000 && unitIndex < units.length - 1) {
|
||||
while (Math.abs(val) < 1 && unitIndex > 0) {
|
||||
val *= 1000;
|
||||
unitIndex--;
|
||||
}
|
||||
while (Math.abs(val) >= 1000 && unitIndex < units.length - 1) {
|
||||
val /= 1000;
|
||||
unitIndex++;
|
||||
}
|
||||
@ -1127,7 +1131,8 @@ export const formatConsumptionShort = (
|
||||
}
|
||||
return (
|
||||
formatNumber(val, hass.locale, {
|
||||
maximumFractionDigits: val < 10 ? 2 : val < 100 ? 1 : 0,
|
||||
maximumFractionDigits:
|
||||
Math.abs(val) < 10 ? 2 : Math.abs(val) < 100 ? 1 : 0,
|
||||
}) +
|
||||
" " +
|
||||
pickedUnit
|
||||
|
@ -68,15 +68,18 @@ describe("Energy Short Format Test", () => {
|
||||
};
|
||||
|
||||
const hass = { locale: defaultLocale } as HomeAssistant;
|
||||
it("Formats", () => {
|
||||
it("No Unit conversion", () => {
|
||||
assert.strictEqual(formatConsumptionShort(hass, 0, "Wh"), "0 Wh");
|
||||
assert.strictEqual(formatConsumptionShort(hass, 0, "kWh"), "0 kWh");
|
||||
assert.strictEqual(formatConsumptionShort(hass, 0, "GWh"), "0 GWh");
|
||||
assert.strictEqual(formatConsumptionShort(hass, 0, "gal"), "0 gal");
|
||||
|
||||
assert.strictEqual(
|
||||
formatConsumptionShort(hass, 0.12345, "kWh"),
|
||||
"0.12 kWh"
|
||||
formatConsumptionShort(hass, 10000.12345, "gal"),
|
||||
"10,000 gal"
|
||||
);
|
||||
|
||||
assert.strictEqual(formatConsumptionShort(hass, 1.2345, "kWh"), "1.23 kWh");
|
||||
assert.strictEqual(
|
||||
formatConsumptionShort(hass, 10.12345, "kWh"),
|
||||
"10.1 kWh"
|
||||
@ -85,6 +88,10 @@ describe("Energy Short Format Test", () => {
|
||||
formatConsumptionShort(hass, 500.12345, "kWh"),
|
||||
"500 kWh"
|
||||
);
|
||||
|
||||
assert.strictEqual(formatConsumptionShort(hass, 10.01, "kWh"), "10 kWh");
|
||||
});
|
||||
it("Upward Unit conversion", () => {
|
||||
assert.strictEqual(
|
||||
formatConsumptionShort(hass, 1512.34567, "kWh"),
|
||||
"1.51 MWh"
|
||||
@ -105,23 +112,31 @@ describe("Energy Short Format Test", () => {
|
||||
formatConsumptionShort(hass, 15123456789.9, "kWh"),
|
||||
"15.1 TWh"
|
||||
);
|
||||
|
||||
assert.strictEqual(
|
||||
formatConsumptionShort(hass, 15123456789000.9, "kWh"),
|
||||
"15,123 TWh"
|
||||
);
|
||||
|
||||
assert.strictEqual(formatConsumptionShort(hass, 1000.1, "GWh"), "1 TWh");
|
||||
|
||||
});
|
||||
it("Downward Unit conversion", () => {
|
||||
assert.strictEqual(formatConsumptionShort(hass, 0.00012, "kWh"), "0.12 Wh");
|
||||
assert.strictEqual(formatConsumptionShort(hass, 0.12345, "kWh"), "123 Wh");
|
||||
assert.strictEqual(
|
||||
formatConsumptionShort(hass, 10000.12345, "gal"),
|
||||
"10,000 gal"
|
||||
formatConsumptionShort(hass, 0.00001234, "TWh"),
|
||||
"12.3 MWh"
|
||||
);
|
||||
});
|
||||
it("Negativ Consumption", () => {
|
||||
assert.strictEqual(
|
||||
formatConsumptionShort(hass, -500.123, "kWh"),
|
||||
"-500 kWh"
|
||||
);
|
||||
|
||||
// Don't really modify negative numbers, but make sure it's something sane.
|
||||
assert.strictEqual(
|
||||
formatConsumptionShort(hass, -1234.56, "kWh"),
|
||||
"-1,234.56 kWh"
|
||||
"-1.23 MWh"
|
||||
);
|
||||
assert.strictEqual(
|
||||
formatConsumptionShort(hass, -0.001234, "kWh"),
|
||||
"-1.23 Wh"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user