Skip to content
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

Support custom unit display in order to handle "$10,000" #218

Open
simonw opened this issue Apr 16, 2018 · 0 comments
Open

Support custom unit display in order to handle "$10,000" #218

simonw opened this issue Apr 16, 2018 · 0 comments

Comments

@simonw
Copy link
Owner

simonw commented Apr 16, 2018

I tried to get Datasette to display $10,000 using the new units support but we currently only display units as a suffix:

datasette/datasette/app.py

Lines 563 to 572 in 10a34f9

if column in table_metadata.get('units', {}) and value != '':
# Interpret units using pint
value = value * ureg(table_metadata['units'][column])
# Pint uses floating point which sometimes introduces errors in the compact
# representation, which we have to round off to avoid ugliness. In the vast
# majority of cases this rounding will be inconsequential. I hope.
value = round(value.to_compact(), 6)
display_value = jinja2.Markup('{:~P}'.format(value).replace(' ', ' '))
else:
display_value = str(value)

It would be neat if there was a mechanism for specifying a custom unit display - maybe something like this:

{
    "custom_units": {
        "us_dollar": {
            "unit": "us_dollar = [] = $",
            "format": "${:,}"
        }
    }
}
@simonw simonw added the small label Apr 16, 2018
@simonw simonw added the feature label Jul 10, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant