Skip to content

Make fluent_number obey format specifiers for currencies #5

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
Nov 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ fluent_compiler 0.3 (under development)

* Fixed test suite on Python 3.8
* Performance improvements
* Made ``fluent_number`` format specifiers for currencies

fluent_compiler 0.2 (2020-04-02)
--------------------------------
Expand Down
2 changes: 1 addition & 1 deletion src/fluent_compiler/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def format(self, locale):
else:
base_pattern = locale.currency_formats['standard']
pattern = self._apply_options(base_pattern)
return pattern.apply(self, locale, currency=self.options.currency)
return pattern.apply(self, locale, currency=self.options.currency, currency_digits=False)

def _apply_options(self, pattern):
# We are essentially trying to copy the
Expand Down
46 changes: 46 additions & 0 deletions tests/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@
from fluent_compiler.types import FluentDateType, FluentNumber, fluent_date, fluent_number


def currency(amount, *args, **kwargs):
return fluent_number(amount,
*args,
currency='USD',
style='currency',
**kwargs)


class TestFluentNumber(unittest.TestCase):

locale = Locale.parse('en_US')
Expand Down Expand Up @@ -143,6 +151,44 @@ def test_currency_display_name(self):
self.assertEqual(cur_pos_name.format(es_GT),
"dólares estadounidenses 123,456.78")

def test_currency_use_grouping(self):
f1 = currency(123456.78, useGrouping=True)
f2 = currency(123456.78, useGrouping=False)
self.assertEqual(f1.format(self.locale), "$123,456.78")
self.assertEqual(f2.format(self.locale), "$123456.78")

def test_currency_minimum_integer_digits(self):
f = currency(1.23, minimumIntegerDigits=3)
self.assertEqual(f.format(self.locale), "$001.23")

def test_currency_minimum_fraction_digits(self):
f = currency(1, minimumFractionDigits=3)
self.assertEqual(f.format(self.locale), "$1.000")
f = currency(1, minimumFractionDigits=0)
self.assertEqual(f.format(self.locale), "$1")

def test_currency_maximum_fraction_digits(self):
f1 = currency(1.23456)
self.assertEqual(f1.format(self.locale), "$1.23")
f2 = currency(1.23456, maximumFractionDigits=5)
self.assertEqual(f2.format(self.locale), "$1.23456")
f2 = currency(1.23456, maximumFractionDigits=0)
self.assertEqual(f2.format(self.locale), "$1")

def test_currency_minimum_significant_digits(self):
f1 = currency(123, minimumSignificantDigits=5)
self.assertEqual(f1.format(self.locale), "$123.00")
f2 = currency(12.3, minimumSignificantDigits=5)
self.assertEqual(f2.format(self.locale), "$12.300")

def test_currency_maximum_significant_digits(self):
f1 = currency(123456, maximumSignificantDigits=3)
self.assertEqual(f1.format(self.locale), "$123,000")
f2 = currency(12.3456, maximumSignificantDigits=3)
self.assertEqual(f2.format(self.locale), "$12.3")
f3 = currency(12, maximumSignificantDigits=5)
self.assertEqual(f3.format(self.locale), "$12")

def test_copy_attributes(self):
f1 = fluent_number(123456.78, useGrouping=False)
self.assertEqual(f1.options.useGrouping, False)
Expand Down