Skip to content

Commit

Permalink
Added more tests and doctests
Browse files Browse the repository at this point in the history
  • Loading branch information
Abdullahjavednesar committed Sep 12, 2020
1 parent 5150e7b commit 4e745b4
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
26 changes: 24 additions & 2 deletions babel/numbers.py
Expand Up @@ -401,12 +401,18 @@ def format_decimal(
u'1.235'
>>> format_decimal(1.2346, locale='en_US', decimal_quantization=False)
u'1.2346'
>>> format_decimal(12345.67, locale='fr_CA', group_separator=False)
u'12345,67'
>>> format_decimal(12345.67, locale='en_US', group_separator=True)
u'12,345.67'
:param number: the number to format
:param format:
:param locale: the `Locale` object or locale identifier
:param decimal_quantization: Truncate and round high-precision numbers to
the format pattern. Defaults to `True`.
:param group_separator: Boolean to switch group separator on/off in a locale's
number format.
"""
locale = Locale.parse(locale)
if not format:
Expand Down Expand Up @@ -472,6 +478,12 @@ def format_currency(
...
UnknownCurrencyFormatError: "'unknown' is not a known currency format type"
>>> format_currency(101299.98, 'EUR', locale='fr_CA', group_separator=False)
u'101299,98\xa0€'
>>> format_currency(101299.98, 'EUR', locale='fr_CA', group_separator=True)
u'101\u202f299,98\xa0€'
You can also pass format_type='name' to use long display names. The order of
the number and currency name, along with the correct localized plural form
of the currency name, is chosen according to locale:
Expand Down Expand Up @@ -500,6 +512,8 @@ def format_currency(
:param format_type: the currency format type to use
:param decimal_quantization: Truncate and round high-precision numbers to
the format pattern. Defaults to `True`.
:param group_separator: Boolean to switch group separator on/off in a locale's
number format.
"""
if format_type == 'name':
Expand Down Expand Up @@ -582,11 +596,19 @@ def format_percent(
>>> format_percent(23.9876, locale='en_US', decimal_quantization=False)
u'2,398.76%'
>>> format_percent(229291.1234, locale='pt_BR', group_separator=False)
u'22929112%'
>>> format_percent(229291.1234, locale='pt_BR', group_separator=True)
u'22.929.112%'
:param number: the percent number to format
:param format:
:param locale: the `Locale` object or locale identifier
:param decimal_quantization: Truncate and round high-precision numbers to
the format pattern. Defaults to `True`.
:param group_separator: Boolean to switch group separator on/off in a locale's
number format.
"""
locale = Locale.parse(locale)
if not format:
Expand All @@ -597,7 +619,7 @@ def format_percent(


def format_scientific(
number, format=None, locale=LC_NUMERIC, decimal_quantization=True, group_separator=True):
number, format=None, locale=LC_NUMERIC, decimal_quantization=True):
"""Return value formatted in scientific notation for a specific locale.
>>> format_scientific(10000, locale='en_US')
Expand Down Expand Up @@ -628,7 +650,7 @@ def format_scientific(
format = locale.scientific_formats.get(format)
pattern = parse_pattern(format)
return pattern.apply(
number, locale, decimal_quantization=decimal_quantization, group_separator=group_separator)
number, locale, decimal_quantization=decimal_quantization)


class NumberFormatError(ValueError):
Expand Down
15 changes: 15 additions & 0 deletions tests/test_numbers.py
Expand Up @@ -168,6 +168,21 @@ def test_group_separator(self):
locale='en_US', group_separator=False, format_type='name'))
self.assertEqual(u'25123412\xa0%', numbers.format_percent(251234.1234, locale='sv_SE', group_separator=False))

self.assertEqual(u'29,567.12', numbers.format_decimal(29567.12,
locale='en_US', group_separator=True))
self.assertEqual(u'29\u202f567,12', numbers.format_decimal(29567.12,
locale='fr_CA', group_separator=True))
self.assertEqual(u'29.567,12', numbers.format_decimal(29567.12,
locale='pt_BR', group_separator=True))
self.assertEqual(u'$1,099.98', numbers.format_currency(1099.98, 'USD',
locale='en_US', group_separator=True))
self.assertEqual(u'101\u202f299,98\xa0\u20ac', numbers.format_currency(101299.98, 'EUR',
locale='fr_CA', group_separator=True))
self.assertEqual(u'101,299.98 euros', numbers.format_currency(101299.98, 'EUR',
locale='en_US', group_separator=True,
format_type='name'))
self.assertEqual(u'25\xa0123\xa0412\xa0%', numbers.format_percent(251234.1234, locale='sv_SE', group_separator=True))


class NumberParsingTestCase(unittest.TestCase):

Expand Down

0 comments on commit 4e745b4

Please sign in to comment.