Skip to content

Added an option to format numbers using toLocaleString() for table columns #807

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
merged 2 commits into from
Feb 11, 2025

Conversation

francesco-cattoglio
Copy link
Contributor

Related to #802

This is the best I could hack, with my limited knowledge and in the time I had to spare. The basic usage is as follows:

  • to select which columns should have their numbers formatted according to user locale, you specify them just like you would do for markdown
  • there is an extra option that allows you to choose the exact number of decimal digits to be used when formatting, if you need such a precision. This is used for the entire table.

E.G: if you need a table with two columns with numbers formatted using exactly three decimals, and a markdown column with links, you would write

select 'table' as component,
    'links'  as markdown,
    'col A' as number_format_locale,
    'col B' as number_format_locale,
    3 as number_format_digits,
    TRUE as sort;

Pros:

  • The changes are very much contained and easy to understand, I do not expect any interference with other features
  • Functionality is limited, but can still be useful!

Cons:

  • Very limited functionality. toLocaleString() can do so much more, and the list of available options is very very long. I am not sure if we should just allow the user to pass in a JSON structure so that he can choose whatever combination is needed (e.g: this can include formatting with a given currency!)
  • no checks w.r.t. the input provided to the number_format_digits field, I have not tested to see what happens if a user inputs anything other than an integer
  • when this feature is used, the whole table cell contents are replaced. In 99% of cases this should not be a problem, but I could see someone requesting a way to only format the number and keep the rest of the contents (e.g: keep a currency symbol after the number).

Let me know what you think about this!

@francesco-cattoglio francesco-cattoglio marked this pull request as draft February 11, 2025 13:04
@lovasoa
Copy link
Collaborator

lovasoa commented Feb 11, 2025

Thank you very much for this contribution ! I'll take things from here. The results look good, I think I'll revert the logic to be opt out rather than opt in (format numbers by default).

@lovasoa lovasoa marked this pull request as ready for review February 11, 2025 22:57
@lovasoa lovasoa merged commit c57d82c into sqlpage:main Feb 11, 2025
10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants