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: 8 additions & 4 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1424,10 +1424,14 @@ def scientific_notation_elements(
value = value * get_decimal_quantum(exp)
assert value.adjusted() == 0

# Shift exponent and value by the minimum number of leading digits
# imposed by the rendering pattern. And always make that number
# greater or equal to 1.
lead_shift = max([1, min(self.int_prec)]) - 1
# Shift exponent and value by the number of leading digits imposed by
# the rendering pattern. If a maximum integer digit count is present
# and larger than the minimum, it specifies exponent grouping.
min_int, max_int = self.int_prec
if max_int > min_int and max_int > 1:
lead_shift = exp % max_int
else:
lead_shift = max([1, min_int]) - 1
exp = exp - lead_shift
value = value * get_decimal_quantum(-lead_shift)

Expand Down
4 changes: 3 additions & 1 deletion tests/test_numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,12 +474,14 @@ def test_format_scientific():
assert numbers.format_scientific(4234567, '#.#E0', locale='en_US') == '4.2E6'
assert numbers.format_scientific(4234567, '0E0000', locale='en_US') == '4.234567E0006'
assert numbers.format_scientific(4234567, '##0E00', locale='en_US') == '4.234567E06'
assert numbers.format_scientific(4234567, '##00E00', locale='en_US') == '42.34567E05'
assert numbers.format_scientific(4234567, '##00E00', locale='en_US') == '423.4567E04'
assert numbers.format_scientific(4234567, '0,000E00', locale='en_US') == '4,234.567E03'
assert numbers.format_scientific(4234567, '##0.#####E00', locale='en_US') == '4.23457E06'
assert numbers.format_scientific(4234567, '##0.##E00', locale='en_US') == '4.23E06'
assert numbers.format_scientific(42, '00000.000000E0000', locale='en_US') == '42000.000000E-0003'
assert numbers.format_scientific(0.2, locale="ar_EG", numbering_system="default") == '2أس\u061c-1'
assert numbers.format_scientific(12345, '##0.####E0', locale='en_US') == '12.345E3'
assert numbers.format_scientific(12345, '##0E00', locale='en_US') == '12.345E03'


def test_default_scientific_format():
Expand Down
2 changes: 1 addition & 1 deletion tests/test_numbers_format_decimal.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_scientific_notation():
assert numbers.format_scientific(1234, '0.###E0', locale='en_US') == '1.234E3'
assert numbers.format_scientific(1234, '0.#E0', locale='en_US') == '1.2E3'
# Exponent grouping
assert numbers.format_scientific(12345, '##0.####E0', locale='en_US') == '1.2345E4'
assert numbers.format_scientific(12345, '##0.####E0', locale='en_US') == '12.345E3'
# Minimum number of int digits
assert numbers.format_scientific(12345, '00.###E0', locale='en_US') == '12.345E3'
assert numbers.format_scientific(-12345.6, '00.###E0', locale='en_US') == '-12.346E3'
Expand Down