Skip to content
Open
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
20 changes: 17 additions & 3 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1517,10 +1517,15 @@ def apply(
if not decimal_quantization or (self.exp_prec and frac_prec == (0, 0)):
frac_prec = (frac_prec[0], max([frac_prec[1], get_decimal_precision(value)]))

decimal_symbol = None
if currency:
symbols = _get_number_symbols(locale, numbering_system=numbering_system)
decimal_symbol = symbols.get('currencyDecimal')

# Render scientific notation.
if self.exp_prec:
number = ''.join([
self._quantize_value(value, locale, frac_prec, group_separator, numbering_system=numbering_system),
self._quantize_value(value, locale, frac_prec, group_separator, numbering_system=numbering_system, decimal_symbol=decimal_symbol),
get_exponential_symbol(locale, numbering_system=numbering_system),
exp_sign, # type: ignore # exp_sign is always defined here
self._format_int(str(exp), self.exp_prec[0], self.exp_prec[1], locale, numbering_system=numbering_system), # type: ignore # exp is always defined here
Expand All @@ -1532,7 +1537,10 @@ def apply(
a, sep, b = text.partition(".")
number = self._format_int(a, 0, 1000, locale, numbering_system=numbering_system)
if sep:
number += get_decimal_symbol(locale, numbering_system=numbering_system) + b
number += (
decimal_symbol
or get_decimal_symbol(locale, numbering_system=numbering_system)
) + b

# A normal number pattern.
else:
Expand All @@ -1542,6 +1550,7 @@ def apply(
frac_prec,
group_separator,
numbering_system=numbering_system,
decimal_symbol=decimal_symbol,
)

retval = ''.join(
Expand Down Expand Up @@ -1631,6 +1640,7 @@ def _quantize_value(
group_separator: bool,
*,
numbering_system: Literal["default"] | str,
decimal_symbol: str | None = None,
) -> str:
# If the number is +/-Infinity, we can't quantize it
if value.is_infinite():
Expand All @@ -1652,6 +1662,7 @@ def _quantize_value(
locale=locale,
force_frac=frac_prec,
numbering_system=numbering_system,
decimal_symbol=decimal_symbol,
)
return number

Expand All @@ -1662,6 +1673,7 @@ def _format_frac(
force_frac: tuple[int, int] | None = None,
*,
numbering_system: Literal["default"] | str,
decimal_symbol: str | None = None,
) -> str:
min, max = force_frac or self.frac_prec
if len(value) < min:
Expand All @@ -1670,4 +1682,6 @@ def _format_frac(
return ''
while len(value) > min and value[-1] == '0':
value = value[:-1]
return get_decimal_symbol(locale, numbering_system=numbering_system) + value
return (
decimal_symbol or get_decimal_symbol(locale, numbering_system=numbering_system)
) + value
20 changes: 20 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,26 @@ def test_format_currency():
== 'US$0,00') # other


@pytest.mark.parametrize('format_type', ['standard', 'accounting', 'name'])
def test_format_currency_decimal_symbol(format_type):
formatted = numbers.format_currency(
'12345.678', 'USD', locale='fr_CH', format_type=format_type,
)
assert formatted.startswith("12'345.68")
assert numbers.format_decimal('12345.678', locale='fr_CH') == "12'345,678"


@pytest.mark.parametrize(('pattern', 'expected'), [
('0.00', '1.23'),
('0.00E0', '1.23E0'),
('@@@', '1.23'),
])
def test_format_currency_decimal_symbol_custom_pattern(pattern, expected):
assert numbers.format_currency(
'1.2345', 'USD', format=pattern, locale='fr_CH',
) == expected


def test_format_currency_format_type():
assert (numbers.format_currency(1099.98, 'USD', locale='en_US',
format_type="standard")
Expand Down