Additional number formatting#7763
Conversation
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
src/common/string/format_number.ts
Outdated
| // 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; | ||
| } |
There was a problem hiding this comment.
@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).
There was a problem hiding this comment.
Then we should only apply it on strings?
There was a problem hiding this comment.
@bramkragten I've updated this function to return early if the value is not a string.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
You can use |
This is not the place for feature requests. Create an issue or discussion. |
This comment has been minimized.
This comment has been minimized.
Stop commenting on this PR. And yes I have. |

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
Example configuration
Add additional Lovelace cards to the UI to see the formatting implemented:
Additional information
Checklist
If user exposed functionality or configuration variables are added/changed: