Skip to content

Commit f25cee0

Browse files
Added more tests and doctests
1 parent 5150e7b commit f25cee0

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

babel/numbers.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,12 +401,18 @@ def format_decimal(
401401
u'1.235'
402402
>>> format_decimal(1.2346, locale='en_US', decimal_quantization=False)
403403
u'1.2346'
404+
>>> format_decimal(12345.67, locale='fr_CA', group_separator=False)
405+
u'12345,67'
406+
>>> format_decimal(12345.67, locale='en_US', group_separator=True)
407+
u'12,345.67'
404408
405409
:param number: the number to format
406410
:param format:
407411
:param locale: the `Locale` object or locale identifier
408412
:param decimal_quantization: Truncate and round high-precision numbers to
409413
the format pattern. Defaults to `True`.
414+
:param group_separator: Boolean to switch group separator on/off in a locale's
415+
number format.
410416
"""
411417
locale = Locale.parse(locale)
412418
if not format:
@@ -472,6 +478,12 @@ def format_currency(
472478
...
473479
UnknownCurrencyFormatError: "'unknown' is not a known currency format type"
474480
481+
>>> format_currency(101299.98, 'EUR', locale='en_US', group_separator=False)
482+
u'\u20ac101299.98'
483+
484+
>>> format_currency(101299.98, 'EUR', locale='en_US', group_separator=True)
485+
u'€101,299.98'
486+
475487
You can also pass format_type='name' to use long display names. The order of
476488
the number and currency name, along with the correct localized plural form
477489
of the currency name, is chosen according to locale:
@@ -500,6 +512,8 @@ def format_currency(
500512
:param format_type: the currency format type to use
501513
:param decimal_quantization: Truncate and round high-precision numbers to
502514
the format pattern. Defaults to `True`.
515+
:param group_separator: Boolean to switch group separator on/off in a locale's
516+
number format.
503517
504518
"""
505519
if format_type == 'name':
@@ -582,11 +596,19 @@ def format_percent(
582596
>>> format_percent(23.9876, locale='en_US', decimal_quantization=False)
583597
u'2,398.76%'
584598
599+
>>> format_percent(229291.1234, locale='pt_BR', group_separator=False)
600+
u'22929112%'
601+
602+
>>> format_percent(229291.1234, locale='pt_BR', group_separator=True)
603+
u'22.929.112%'
604+
585605
:param number: the percent number to format
586606
:param format:
587607
:param locale: the `Locale` object or locale identifier
588608
:param decimal_quantization: Truncate and round high-precision numbers to
589609
the format pattern. Defaults to `True`.
610+
:param group_separator: Boolean to switch group separator on/off in a locale's
611+
number format.
590612
"""
591613
locale = Locale.parse(locale)
592614
if not format:
@@ -597,7 +619,7 @@ def format_percent(
597619

598620

599621
def format_scientific(
600-
number, format=None, locale=LC_NUMERIC, decimal_quantization=True, group_separator=True):
622+
number, format=None, locale=LC_NUMERIC, decimal_quantization=True):
601623
"""Return value formatted in scientific notation for a specific locale.
602624
603625
>>> format_scientific(10000, locale='en_US')
@@ -628,7 +650,7 @@ def format_scientific(
628650
format = locale.scientific_formats.get(format)
629651
pattern = parse_pattern(format)
630652
return pattern.apply(
631-
number, locale, decimal_quantization=decimal_quantization, group_separator=group_separator)
653+
number, locale, decimal_quantization=decimal_quantization)
632654

633655

634656
class NumberFormatError(ValueError):

tests/test_numbers.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,21 @@ def test_group_separator(self):
168168
locale='en_US', group_separator=False, format_type='name'))
169169
self.assertEqual(u'25123412\xa0%', numbers.format_percent(251234.1234, locale='sv_SE', group_separator=False))
170170

171+
self.assertEqual(u'29,567.12', numbers.format_decimal(29567.12,
172+
locale='en_US', group_separator=True))
173+
self.assertEqual(u'29\u202f567,12', numbers.format_decimal(29567.12,
174+
locale='fr_CA', group_separator=True))
175+
self.assertEqual(u'29.567,12', numbers.format_decimal(29567.12,
176+
locale='pt_BR', group_separator=True))
177+
self.assertEqual(u'$1,099.98', numbers.format_currency(1099.98, 'USD',
178+
locale='en_US', group_separator=True))
179+
self.assertEqual(u'101\u202f299,98\xa0\u20ac', numbers.format_currency(101299.98, 'EUR',
180+
locale='fr_CA', group_separator=True))
181+
self.assertEqual(u'101,299.98 euros', numbers.format_currency(101299.98, 'EUR',
182+
locale='en_US', group_separator=True,
183+
format_type='name'))
184+
self.assertEqual(u'25\xa0123\xa0412\xa0%', numbers.format_percent(251234.1234, locale='sv_SE', group_separator=True))
185+
171186

172187
class NumberParsingTestCase(unittest.TestCase):
173188

0 commit comments

Comments
 (0)