Skip to content

Commit

Permalink
feat: Format numbers according to selected language [#495 #509]
Browse files Browse the repository at this point in the history
Fixes #495 #509
  • Loading branch information
RomRider committed Jan 17, 2021
1 parent e889848 commit 875b557
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
23 changes: 21 additions & 2 deletions .devcontainer/configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,24 @@ lovelace:
type: module

default_config:

demo:

demo:

sensor:
- platform: template
sensors:
pressure:
friendly_name: "Pressure"
unit_of_measurement: "hPa"
value_template: "{{ state_attr('weather.home', 'pressure') }}"
device_class: pressure
temperature:
friendly_name: "Temperature"
unit_of_measurement: "°C"
value_template: "{{ state_attr('weather.home', 'temperature') }}"
device_class: temperature
humidity:
friendly_name: "Humidity"
unit_of_measurement: "%"
value_template: "{{ state_attr('weather.home', 'humidity') }}"
device_class: humidity
11 changes: 10 additions & 1 deletion .devcontainer/ui-lovelace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,13 @@ views:
cards:
- type: custom:mini-graph-card
entities:
- sensor.outside_humidity
- sensor.humidity
- type: custom:mini-graph-card
entities:
- sensor.temperature
- type: custom:mini-graph-card
entities:
- sensor.pressure
show:
extrema: true
# decimals: 10
21 changes: 18 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,11 +667,26 @@ class MiniGraphCard extends LitElement {
const dec = this.config.decimals;
const value_factor = 10 ** this.config.value_factor;

if (dec === undefined || Number.isNaN(dec) || Number.isNaN(state))
return Math.round(state * value_factor * 100) / 100;
if (dec === undefined || Number.isNaN(dec) || Number.isNaN(state)) {
return this.numberFormat(Math.round(state * value_factor * 100) / 100, this._hass.language);
}

const x = 10 ** dec;
return (Math.round(state * value_factor * x) / x).toFixed(dec);
return this.numberFormat(
(Math.round(state * value_factor * x) / x).toFixed(dec),
this._hass.language, dec,
);
}

numberFormat(num, language, dec) {
if (!Number.isNaN(Number(num)) && Intl) {
if (dec === undefined || Number.isNaN(dec)) {
return new Intl.NumberFormat(language).format(Number(num));
} else {
return new Intl.NumberFormat(language, { minimumFractionDigits: dec }).format(Number(num));
}
}
return num.toString();
}

updateOnInterval() {
Expand Down

0 comments on commit 875b557

Please sign in to comment.