Skip to content

Additional number formatting#7763

Merged
bramkragten merged 17 commits intohome-assistant:devfrom
joshmcrty:additional-number-formatting
Nov 25, 2020
Merged

Additional number formatting#7763
bramkragten merged 17 commits intohome-assistant:devfrom
joshmcrty:additional-number-formatting

Conversation

@joshmcrty
Copy link
Contributor

Proposed change

This adds number formatting to more areas of the UI based on the user's selected language/locale to be consistent with the changes in #7516.

Type of change

  • Dependency upgrade
  • Bugfix (non-breaking change which fixes an issue)
  • New feature (thank you!)
  • Breaking change (fix/feature causing existing functionality to break)
  • Code quality improvements to existing code or addition of tests

Example configuration

Add additional Lovelace cards to the UI to see the formatting implemented:

  • Entity
  • Gauge
  • Weather
  • Thermostat

Additional information

Checklist

  • The code change is tested and works locally.
  • There is no commented out code in this PR.
  • Tests have been added to verify that the new code works.

If user exposed functionality or configuration variables are added/changed:

This can be helpful if a sensor stores the internal value as a formatted number string (for example USD currency) and should resolve home-assistant#7758
Comment on lines +39 to +50
// Keep decimal trailing zeros if they are present
if (
!options ||
(!options.minimumFractionDigits && !options.maximumFractionDigits)
) {
const digits =
num.toString().indexOf(".") > -1
? num.toString().split(".")[1].length
: 0;
defaultOptions.minimumFractionDigits = digits;
defaultOptions.maximumFractionDigits = digits;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the use case here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bramkragten the current implementation accepts a number or a string. This ensures it works with state values (which are type string in HassEntityBase) and attributes which can be type number. If a sensor intentionally stores a numeric value with a trailing zero (see sensor.processor_temperature value in #7787), this code would preserve the decimals instead of dropping the trailing zero(s).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Then we should only apply it on strings?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bramkragten I've updated this function to return early if the value is not a string.

@krakers09

This comment has been minimized.

@KNXBroker

This comment has been minimized.

@bramkragten
Copy link
Member

This should include possibility of setting formatting by user. Forcing users using HA in English to use this formatting is a step backward.

You can use English GB and this is not the place for feature requests.

@bramkragten
Copy link
Member

Hello,

I have recognized that decimal 0s are not displayed in gauge cards, while they are displayed on enities cards. See below for an example. It would be great if you could fix this with this PR as well (guess it not difficult...)

Unbenannt

Best regards

This is not the place for feature requests. Create an issue or discussion.

@krakers09

This comment has been minimized.

@bramkragten
Copy link
Member

This should include possibility of setting formatting by user. Forcing users using HA in English to use this formatting is a step backward.

You can use English GB and this is not the place for feature requests.

Have you even checked that before answering? I really doubt so...

Stop commenting on this PR. And yes I have.

@joshmcrty
Copy link
Contributor Author

7dd7ce1 should fix #7795

@Mariusthvdb

This comment has been minimized.

@bramkragten bramkragten merged commit 7403405 into home-assistant:dev Nov 25, 2020
@github-actions github-actions bot locked and limited conversation to collaborators Nov 26, 2020
@joshmcrty joshmcrty deleted the additional-number-formatting branch December 2, 2020 05:14
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants