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
12 changes: 12 additions & 0 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1519,6 +1519,18 @@ def apply(

# Render scientific notation.
if self.exp_prec:
if value.is_finite():
rounded = value.quantize(get_decimal_quantum(frac_prec[1]))
# Rounding can add a leading digit to the mantissa. Shift it
# back to the pattern's required width and adjust the exponent.
if rounded.adjusted() > value.adjusted():
value = rounded.scaleb(-1)
if exp_sign == get_minus_sign_symbol(locale, numbering_system=numbering_system):
exp -= 1
if exp == 0:
exp_sign = get_plus_sign_symbol(locale, numbering_system=numbering_system) if self.exp_plus else ''
else:
exp += 1
number = ''.join([
self._quantize_value(value, locale, frac_prec, group_separator, numbering_system=numbering_system),
get_exponential_symbol(locale, numbering_system=numbering_system),
Expand Down
41 changes: 41 additions & 0 deletions tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,47 @@ def test_format_scientific():
assert numbers.format_scientific(0.2, locale="ar_EG", numbering_system="default") == '2أس\u061c-1'


@pytest.mark.parametrize(('value', 'pattern', 'expected'), [
('9.99', '0.0E0', '1.0E1'),
('0.999', '0.0E0', '1.0E0'),
('0.0999', '0.0E0', '1.0E-1'),
('9999', '0.0E0', '1.0E4'),
('-9.99', '0.0E0', '-1.0E1'),
('9.99', '0.0E+00', '1.0E+01'),
('0.999', '0.0E+00', '1.0E+00'),
('99.99', '00.0E0', '10.0E1'),
('9.94', '0.0E0', '9.9E0'),
('0', '0.0E0', '0.0E0'),
('Infinity', '0.0E0', '\u221eE0'),
])
def test_format_scientific_rounding_carry(value, pattern, expected):
assert numbers.format_scientific(decimal.Decimal(value), pattern, locale='en_US') == expected


@pytest.mark.parametrize(('rounding', 'expected'), [
(decimal.ROUND_UP, '1.0E1'),
(decimal.ROUND_DOWN, '9.9E0'),
])
def test_format_scientific_rounding_carry_context(rounding, expected):
with decimal.localcontext(rounding=rounding):
assert numbers.format_scientific(decimal.Decimal('9.99'), '0.0E0', locale='en_US') == expected


def test_format_scientific_rounding_carry_numbering_system():
assert numbers.format_scientific(
decimal.Decimal('0.0999'), '0.0E0', locale='ar_EG', numbering_system='default',
) == '1\u066b0\u0623\u0633\u061c-1'
assert numbers.format_scientific(
decimal.Decimal('0.999'), '0.0E+0', locale='ar_EG', numbering_system='default',
) == '1\u066b0\u0623\u0633\u061c+0'


def test_format_scientific_rounding_carry_without_quantization():
assert numbers.format_scientific(
decimal.Decimal('9.99'), '0.0E0', locale='en_US', decimal_quantization=False,
) == '9.99E0'


def test_default_scientific_format():
"""Check the scientific format method auto-correct the rendering pattern
in case of a missing fractional part.
Expand Down