-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Additional number formatting #7763
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
bramkragten
merged 17 commits into
home-assistant:dev
from
joshmcrty:additional-number-formatting
Nov 25, 2020
Merged
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
feecaff
Make file and func names consistent with date format files and funcs
joshmcrty fe66d8e
Add number formatting in badges for sensors with units of measurement.
joshmcrty 56821f4
Add number formatting to entity card
joshmcrty e29b4f3
Add number formatting to weather card
joshmcrty bdfc3e9
Add number formatting to weather more info dialog
joshmcrty 033621d
Add number formatting to gauge card
joshmcrty 921a13e
Enable passing Intl.NumberFormatOptions to formatNumber function
joshmcrty ee6daa3
Add number formatting to thermostat card
joshmcrty 74eff87
Keep trailing zeros in decimals in formatNumber
joshmcrty 9c8bbf6
Fix lint error
joshmcrty 60b0e75
Replace toFixed with formatNumber options
joshmcrty 0dca26c
Handle rounding with formatNumber options
joshmcrty 516d528
Move formatNumber into ha-gauge element
joshmcrty 2b69302
Remove unused imports
joshmcrty 7dd7ce1
Format numbers in climate entity row
joshmcrty b691793
Only check for trailing decimal zeros on strings
joshmcrty b4b4a06
Format number for sun elevation in more info dialog
joshmcrty File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| /** | ||
| * Formats a number based on the specified language with thousands separator(s) and decimal character for better legibility. | ||
| * | ||
| * @param num The number to format | ||
| * @param language The language to use when formatting the number | ||
| */ | ||
| export const formatNumber = ( | ||
| num: string | number, | ||
| language: string, | ||
| options?: Intl.NumberFormatOptions | ||
| ): string => { | ||
| // Polyfill for Number.isNaN, which is more reliable than the global isNaN() | ||
| Number.isNaN = | ||
| Number.isNaN || | ||
| function isNaN(input) { | ||
| return typeof input === "number" && isNaN(input); | ||
| }; | ||
|
|
||
| if (!Number.isNaN(Number(num)) && Intl) { | ||
| return new Intl.NumberFormat( | ||
| language, | ||
| getDefaultFormatOptions(num, options) | ||
| ).format(Number(num)); | ||
| } | ||
| return num.toString(); | ||
| }; | ||
|
|
||
| /** | ||
| * Generates default options for Intl.NumberFormat | ||
| * @param num The number to be formatted | ||
| * @param options The Intl.NumberFormatOptions that should be included in the returned options | ||
| */ | ||
| const getDefaultFormatOptions = ( | ||
| num: string | number, | ||
| options?: Intl.NumberFormatOptions | ||
| ): Intl.NumberFormatOptions => { | ||
| let defaultOptions: Intl.NumberFormatOptions = options || {}; | ||
|
|
||
| // 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; | ||
| } | ||
|
|
||
| return defaultOptions; | ||
| }; | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
numberor astring. This ensures it works with state values (which are typestringinHassEntityBase) and attributes which can be typenumber. If a sensor intentionally stores a numeric value with a trailing zero (seesensor.processor_temperaturevalue in #7787), this code would preserve the decimals instead of dropping the trailing zero(s).There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.