From 9bb36e38e6cf7a55f3f176bbc6437d08a413235b Mon Sep 17 00:00:00 2001 From: Paul Bottein Date: Mon, 6 Mar 2023 09:53:53 +0100 Subject: [PATCH] Fix number format for monetary device class (#15693) --- src/common/entity/compute_state_display.ts | 5 +++++ src/common/number/format_number.ts | 9 +++------ 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/common/entity/compute_state_display.ts b/src/common/entity/compute_state_display.ts index cb327710c2..930d8added 100644 --- a/src/common/entity/compute_state_display.ts +++ b/src/common/entity/compute_state_display.ts @@ -71,6 +71,11 @@ export const computeStateDisplayFromEntityAttributes = ( style: "currency", currency: attributes.unit_of_measurement, minimumFractionDigits: 2, + // Override monetary options with number format + ...getNumberFormatOptions( + { state, attributes } as HassEntity, + entity + ), }); } catch (_err) { // fallback to default diff --git a/src/common/number/format_number.ts b/src/common/number/format_number.ts index 8efd88c373..dcbcd72d02 100644 --- a/src/common/number/format_number.ts +++ b/src/common/number/format_number.ts @@ -82,17 +82,14 @@ export const formatNumber = ( !Number.isNaN(Number(num)) && num !== "" && localeOptions?.number_format === NumberFormat.none && - Intl && - (options?.maximumFractionDigits != null || - options?.minimumFractionDigits != null) + Intl ) { - // If NumberFormat is none, just set the digits options for precision and use en-US format without grouping. + // If NumberFormat is none, use en-US format without grouping. return new Intl.NumberFormat( "en-US", getDefaultFormatOptions(num, { + ...options, useGrouping: false, - maximumFractionDigits: options?.maximumFractionDigits, - minimumFractionDigits: options?.minimumFractionDigits, }) ).format(Number(num)); }