gh-87790: support thousands separators for formatting fractional part of floats (#125304)

```pycon
>>> f"{123_456.123_456:_._f}"  # Whole and fractional
'123_456.123_456'
>>> f"{123_456.123_456:_f}"    # Integer component only
'123_456.123456'
>>> f"{123_456.123_456:._f}"   # Fractional component only
'123456.123_456'
>>> f"{123_456.123_456:.4_f}"  # with precision
'123456.1_235'
```
This commit is contained in:
Sergey B Kirpichev 2025-02-25 18:27:07 +03:00 committed by GitHub
parent fa6a8140dd
commit f39a07be47
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 218 additions and 45 deletions

View file

@ -515,11 +515,15 @@ class FormatTest(unittest.TestCase):
error_msg = re.escape("Cannot specify both ',' and '_'.")
with self.assertRaisesRegex(ValueError, error_msg):
'{:,_}'.format(1)
with self.assertRaisesRegex(ValueError, error_msg):
'{:.,_f}'.format(1.1)
def test_with_an_underscore_and_a_comma_in_format_specifier(self):
error_msg = re.escape("Cannot specify both ',' and '_'.")
with self.assertRaisesRegex(ValueError, error_msg):
'{:_,}'.format(1)
with self.assertRaisesRegex(ValueError, error_msg):
'{:._,f}'.format(1.1)
def test_better_error_message_format(self):
# https://bugs.python.org/issue20524