diff options
author | Sergey B Kirpichev <skirpichev@gmail.com> | 2025-02-25 15:27:07 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-02-25 15:27:07 (GMT) |
commit | f39a07be47cd9219eaf0e538ae32ad8239c88e66 (patch) | |
tree | 1b57568378401b44c9ffd8d3f237b13694842b5c /Objects/unicodeobject.c | |
parent | fa6a8140dd2a72da6df2a7dfafbf07045debf64d (diff) | |
download | cpython-f39a07be47cd9219eaf0e538ae32ad8239c88e66.zip cpython-f39a07be47cd9219eaf0e538ae32ad8239c88e66.tar.gz cpython-f39a07be47cd9219eaf0e538ae32ad8239c88e66.tar.bz2 |
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'
```
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 03f15b3..0686276 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -9772,7 +9772,8 @@ _PyUnicode_InsertThousandsGrouping( Py_ssize_t min_width, const char *grouping, PyObject *thousands_sep, - Py_UCS4 *maxchar) + Py_UCS4 *maxchar, + int forward) { min_width = Py_MAX(0, min_width); if (writer) { @@ -9809,14 +9810,14 @@ _PyUnicode_InsertThousandsGrouping( should be an empty string */ assert(!(grouping[0] == CHAR_MAX && thousands_sep_len != 0)); - digits_pos = d_pos + n_digits; + digits_pos = d_pos + (forward ? 0 : n_digits); if (writer) { - buffer_pos = writer->pos + n_buffer; + buffer_pos = writer->pos + (forward ? 0 : n_buffer); assert(buffer_pos <= PyUnicode_GET_LENGTH(writer->buffer)); assert(digits_pos <= PyUnicode_GET_LENGTH(digits)); } else { - buffer_pos = n_buffer; + buffer_pos = forward ? 0 : n_buffer; } if (!writer) { @@ -9838,7 +9839,7 @@ _PyUnicode_InsertThousandsGrouping( digits, &digits_pos, n_chars, n_zeros, use_separator ? thousands_sep : NULL, - thousands_sep_len, maxchar); + thousands_sep_len, maxchar, forward); /* Use a separator next time. */ use_separator = 1; @@ -9867,7 +9868,7 @@ _PyUnicode_InsertThousandsGrouping( digits, &digits_pos, n_chars, n_zeros, use_separator ? thousands_sep : NULL, - thousands_sep_len, maxchar); + thousands_sep_len, maxchar, forward); } return count; } |