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

feat: Support for short compact currency formats #926

Merged
merged 17 commits into from
Nov 4, 2022

Conversation

DenverCoder1
Copy link
Contributor

@DenverCoder1 DenverCoder1 commented Nov 2, 2022

Added a way to format currency with standard "short" currency formats from the CLDR data.

Closes #923

>>> format_compact_currency(123456789, 'USD', locale='en_US', fraction_digits=2)
u'$123.46M'

>>> format_compact_currency(-123456789, 'USD', locale='en_US', fraction_digits=2)
u'-$123.46M'

>>> Format("en_US").compact_currency(123456789, 'USD', fraction_digits=2)
u'$123.46M'

"short" is currently the only length type that seems to appear in the data.

Note: "standard:short" no longer appears in locale.currency_formats (see #923). This was never documented or tested and did not format things properly in the first place, so I don't think this should be considered a breaking change.

@akx akx added this to the Babel 2.12 milestone Nov 2, 2022
@codecov
Copy link

codecov bot commented Nov 2, 2022

Codecov Report

Merging #926 (cdb54de) into master (3add2c1) will increase coverage by 0.04%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##           master     #926      +/-   ##
==========================================
+ Coverage   91.55%   91.59%   +0.04%     
==========================================
  Files          21       21              
  Lines        4215     4238      +23     
==========================================
+ Hits         3859     3882      +23     
  Misses        356      356              
Impacted Files Coverage Δ
babel/core.py 96.40% <100.00%> (+0.03%) ⬆️
babel/numbers.py 97.93% <100.00%> (+0.10%) ⬆️
babel/support.py 85.08% <100.00%> (+0.12%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

babel/numbers.py Outdated Show resolved Hide resolved
scripts/import_cldr.py Show resolved Hide resolved
babel/numbers.py Outdated Show resolved Hide resolved
@DenverCoder1
Copy link
Contributor Author

DenverCoder1 commented Nov 2, 2022

As far as I can tell, CLDR doesn't provide a format for formatting currency in short form for numbers less than 1000.

As a fallback, maybe it would make sense to take the smallest magnitude and remove characters not related to the ordering of the currency symbol and spacing.

Here is what I currently think could be a good fallback:

# find first format that has a currency symbol
for magnitude in compact_format['other']:
    format = compact_format['other'][magnitude].pattern
    if '¤' not in format:
        continue
    # remove characters that are not the currency symbol, 0's or spaces
    format = re.sub(r'[^0\s\¤]', '', format)
    # compress adjacent spaces into one
    format = re.sub(r'(\s)\s+', r'\1', format).strip()
    break

Example:

en_US: 1234 = '€1K' || 123 = '€123'
es: 1234 = '1 mil €' || 123 = '123 €'

I've made a gist to show the results this produces for all locales:

https://gist.github.com/DenverCoder1/e4a1ab9ac94e576a34ad7371f3573e0b

babel/numbers.py Outdated Show resolved Hide resolved
Copy link
Member

@akx akx left a comment

Choose a reason for hiding this comment

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

Great stuff, thanks @DenverCoder1 and @jun66j5!

@akx akx merged commit 5fcc253 into python-babel:master Nov 4, 2022
@DenverCoder1 DenverCoder1 deleted the compact-currency branch November 4, 2022 17:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

format_currency with standard:short format does not format correctly
3 participants