mirror of
https://github.com/home-assistant/frontend.git
synced 2025-07-18 23:06:40 +00:00
Use display precision for number format none (#15674)
* Use display precision for number format none * Update src/common/number/format_number.ts Co-authored-by: Bram Kragten <mail@bramkragten.nl> * prettier * Fix 0 value --------- Co-authored-by: Bram Kragten <mail@bramkragten.nl>
This commit is contained in:
parent
000e2ef7fc
commit
afe1b7ef59
@ -77,6 +77,26 @@ export const formatNumber = (
|
|||||||
).format(Number(num));
|
).format(Number(num));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (
|
||||||
|
!Number.isNaN(Number(num)) &&
|
||||||
|
num !== "" &&
|
||||||
|
localeOptions?.number_format === NumberFormat.none &&
|
||||||
|
Intl &&
|
||||||
|
(options?.maximumFractionDigits != null ||
|
||||||
|
options?.minimumFractionDigits != null)
|
||||||
|
) {
|
||||||
|
// If NumberFormat is none, just set the digits options for precision and use en-US format without grouping.
|
||||||
|
return new Intl.NumberFormat(
|
||||||
|
"en-US",
|
||||||
|
getDefaultFormatOptions(num, {
|
||||||
|
useGrouping: false,
|
||||||
|
maximumFractionDigits: options?.maximumFractionDigits,
|
||||||
|
minimumFractionDigits: options?.minimumFractionDigits,
|
||||||
|
})
|
||||||
|
).format(Number(num));
|
||||||
|
}
|
||||||
|
|
||||||
if (typeof num === "string") {
|
if (typeof num === "string") {
|
||||||
return num;
|
return num;
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import { HassEntity } from "home-assistant-js-websocket";
|
import { HassEntity } from "home-assistant-js-websocket";
|
||||||
import { PropertyValues } from "lit";
|
import { PropertyValues } from "lit";
|
||||||
import { EntityRegistryEntry } from "../../../data/entity_registry";
|
import { EntityRegistryDisplayEntry } from "../../../data/entity_registry";
|
||||||
import { HomeAssistant } from "../../../types";
|
import { HomeAssistant } from "../../../types";
|
||||||
import { processConfigEntities } from "./process-config-entities";
|
import { processConfigEntities } from "./process-config-entities";
|
||||||
|
|
||||||
@ -37,24 +37,19 @@ function compareEntityState(
|
|||||||
return oldState !== newState;
|
return oldState !== newState;
|
||||||
}
|
}
|
||||||
|
|
||||||
function compareEntityEntryOptions(
|
function compareEntityDisplayEntry(
|
||||||
oldHass: HomeAssistant,
|
oldHass: HomeAssistant,
|
||||||
newHass: HomeAssistant,
|
newHass: HomeAssistant,
|
||||||
entityId: string
|
entityId: string
|
||||||
) {
|
) {
|
||||||
const oldEntry = oldHass.entities[entityId] as
|
const oldEntry = oldHass.entities[entityId] as
|
||||||
| EntityRegistryEntry
|
| EntityRegistryDisplayEntry
|
||||||
| undefined;
|
| undefined;
|
||||||
const newEntry = newHass.entities[entityId] as
|
const newEntry = newHass.entities[entityId] as
|
||||||
| EntityRegistryEntry
|
| EntityRegistryDisplayEntry
|
||||||
| undefined;
|
| undefined;
|
||||||
|
|
||||||
return (
|
return oldEntry?.display_precision !== newEntry?.display_precision;
|
||||||
oldEntry?.options?.sensor?.display_precision !==
|
|
||||||
newEntry?.options?.sensor?.display_precision ||
|
|
||||||
oldEntry?.options?.sensor?.suggested_display_precision !==
|
|
||||||
newEntry?.options?.sensor?.suggested_display_precision
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if config or Entity changed
|
// Check if config or Entity changed
|
||||||
@ -71,7 +66,7 @@ export function hasConfigOrEntityChanged(
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
compareEntityState(oldHass, newHass, element._config!.entity) ||
|
compareEntityState(oldHass, newHass, element._config!.entity) ||
|
||||||
compareEntityEntryOptions(oldHass, newHass, element._config!.entity)
|
compareEntityDisplayEntry(oldHass, newHass, element._config!.entity)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -96,7 +91,7 @@ export function hasConfigOrEntitiesChanged(
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
compareEntityState(oldHass, newHass, entity.entity) ||
|
compareEntityState(oldHass, newHass, entity.entity) ||
|
||||||
compareEntityEntryOptions(oldHass, newHass, entity.entity)
|
compareEntityDisplayEntry(oldHass, newHass, entity.entity)
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -69,6 +69,33 @@ describe("formatNumber", () => {
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("Formats number with fraction digits options if number format is none", () => {
|
||||||
|
assert.strictEqual(
|
||||||
|
formatNumber(
|
||||||
|
1234.5,
|
||||||
|
{ ...defaultLocale, number_format: NumberFormat.none },
|
||||||
|
{
|
||||||
|
minimumFractionDigits: 2,
|
||||||
|
maximumFractionDigits: 2,
|
||||||
|
}
|
||||||
|
),
|
||||||
|
"1234.50"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("Do not formats number with others options if number format is none", () => {
|
||||||
|
assert.strictEqual(
|
||||||
|
formatNumber(
|
||||||
|
1234.5,
|
||||||
|
{ ...defaultLocale, number_format: NumberFormat.none },
|
||||||
|
{
|
||||||
|
useGrouping: true,
|
||||||
|
}
|
||||||
|
),
|
||||||
|
"1234.5"
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
it("Sets only the maximumFractionDigits format option when none are provided for a number value", () => {
|
it("Sets only the maximumFractionDigits format option when none are provided for a number value", () => {
|
||||||
assert.deepEqual(getDefaultFormatOptions(1234.5), {
|
assert.deepEqual(getDefaultFormatOptions(1234.5), {
|
||||||
maximumFractionDigits: 2,
|
maximumFractionDigits: 2,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user