Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2352bd4

Browse files
authored
gh-130860: Fix width calculation, when separators in fractional part (GH-130865)
This amends f39a07b
1 parent 10cdd7f commit 2352bd4

File tree

2 files changed

+34
-17
lines changed

2 files changed

+34
-17
lines changed

Lib/test/test_float.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -768,6 +768,22 @@ def test_format(self):
768768
self.assertEqual(format(x, '<21._f'), '123456.123_456 ')
769769
self.assertEqual(format(x, '+.11_e'), '+1.234_561_234_56e+05')
770770
self.assertEqual(format(x, '+.11,e'), '+1.234,561,234,56e+05')
771+
self.assertEqual(format(x, '021_._f'), '0_000_123_456.123_456')
772+
self.assertEqual(format(x, '020_._f'), '0_000_123_456.123_456')
773+
self.assertEqual(format(x, '+021_._f'), '+0_000_123_456.123_456')
774+
self.assertEqual(format(x, '21_._f'), ' 123_456.123_456')
775+
self.assertEqual(format(x, '>021_._f'), '000000123_456.123_456')
776+
self.assertEqual(format(x, '<021_._f'), '123_456.123_456000000')
777+
self.assertEqual(format(x, '023_.10_f'), '0_123_456.123_456_000_0')
778+
self.assertEqual(format(x, '022_.10_f'), '0_123_456.123_456_000_0')
779+
self.assertEqual(format(x, '+023_.10_f'), '+0_123_456.123_456_000_0')
780+
self.assertEqual(format(x, '023_.9_f'), '000_123_456.123_456_000')
781+
self.assertEqual(format(x, '021_._e'), '0_000_001.234_561e+05')
782+
self.assertEqual(format(x, '020_._e'), '0_000_001.234_561e+05')
783+
self.assertEqual(format(x, '+021_._e'), '+0_000_001.234_561e+05')
784+
self.assertEqual(format(x, '023_.10_e'), '0_001.234_561_234_6e+05')
785+
self.assertEqual(format(x, '022_.10_e'), '0_001.234_561_234_6e+05')
786+
self.assertEqual(format(x, '023_.9_e'), '000_001.234_561_235e+05')
771787

772788
self.assertRaises(ValueError, format, x, '._6f')
773789
self.assertRaises(ValueError, format, x, '.,_f')

Python/formatter_unicode.c

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -578,14 +578,31 @@ calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
578578
}
579579
}
580580

581+
if (spec->n_frac == 0) {
582+
spec->n_grouped_frac_digits = 0;
583+
}
584+
else {
585+
Py_UCS4 grouping_maxchar;
586+
spec->n_grouped_frac_digits = _PyUnicode_InsertThousandsGrouping(
587+
NULL, 0,
588+
NULL, 0, spec->n_frac,
589+
spec->n_frac,
590+
locale->grouping, locale->frac_thousands_sep, &grouping_maxchar, 1);
591+
if (spec->n_grouped_frac_digits == -1) {
592+
return -1;
593+
}
594+
*maxchar = Py_MAX(*maxchar, grouping_maxchar);
595+
}
596+
581597
/* The number of chars used for non-digits and non-padding. */
582598
n_non_digit_non_padding = spec->n_sign + spec->n_prefix + spec->n_decimal +
583599
+ spec->n_frac + spec->n_remainder;
584600

585601
/* min_width can go negative, that's okay. format->width == -1 means
586602
we don't care. */
587603
if (format->fill_char == '0' && format->align == '=')
588-
spec->n_min_width = format->width - n_non_digit_non_padding;
604+
spec->n_min_width = (format->width - n_non_digit_non_padding
605+
+ spec->n_frac - spec->n_grouped_frac_digits);
589606
else
590607
spec->n_min_width = 0;
591608

@@ -607,22 +624,6 @@ calc_number_widths(NumberFieldWidths *spec, Py_ssize_t n_prefix,
607624
*maxchar = Py_MAX(*maxchar, grouping_maxchar);
608625
}
609626

610-
if (spec->n_frac == 0) {
611-
spec->n_grouped_frac_digits = 0;
612-
}
613-
else {
614-
Py_UCS4 grouping_maxchar;
615-
spec->n_grouped_frac_digits = _PyUnicode_InsertThousandsGrouping(
616-
NULL, 0,
617-
NULL, 0, spec->n_frac,
618-
spec->n_frac,
619-
locale->grouping, locale->frac_thousands_sep, &grouping_maxchar, 1);
620-
if (spec->n_grouped_frac_digits == -1) {
621-
return -1;
622-
}
623-
*maxchar = Py_MAX(*maxchar, grouping_maxchar);
624-
}
625-
626627
/* Given the desired width and the total of digit and non-digit
627628
space we consume, see if we need any padding. format->width can
628629
be negative (meaning no padding), but this code still works in

0 commit comments

Comments
 (0)