bpo-45995: add "z" format specifer to coerce negative 0 to zero (GH-30049)

Add "z" format specifier to coerce negative 0 to zero.

See https://github.com/python/cpython/issues/90153 (originally https://bugs.python.org/issue45995) for discussion.
This covers `str.format()` and f-strings.  Old-style string interpolation is not supported.

Co-authored-by: Mark Dickinson <dickinsm@gmail.com>
This commit is contained in:
John Belmonte 2022-04-11 23:34:18 +09:00 committed by GitHub
parent dd207a6ac5
commit b0b836b20c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 368 additions and 43 deletions

View file

@ -309,7 +309,7 @@ non-empty format specification typically modifies the result.
The general form of a *standard format specifier* is:
.. productionlist:: format-spec
format_spec: [[`fill`]`align`][`sign`][#][0][`width`][`grouping_option`][.`precision`][`type`]
format_spec: [[`fill`]`align`][`sign`][z][#][0][`width`][`grouping_option`][.`precision`][`type`]
fill: <any character>
align: "<" | ">" | "=" | "^"
sign: "+" | "-" | " "
@ -380,6 +380,15 @@ following:
+---------+----------------------------------------------------------+
.. index:: single: z; in string formatting
The ``'z'`` option coerces negative zero floating-point values to positive
zero after rounding to the format precision. This option is only valid for
floating-point presentation types.
.. versionchanged:: 3.11
Added the ``'z'`` option (see also :pep:`682`).
.. index:: single: # (hash); in string formatting
The ``'#'`` option causes the "alternate form" to be used for the