Skip to content

Documentation doesn't specify how "new style" formatting works for complex numbers #122767

Closed
@skirpichev

Description

@skirpichev

Old string formatting has no support for complex numbers:

>>> "%f" % (1+0j)
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    "%f" % (1+0j)
    ~~~~~^~~~~~~~
TypeError: must be real number, not complex

But formatted string literals and str.format() have:

>>> f"{1+0j:f}"
'1.000000+0.000000j'
>>> f"{1+0j:}"
'(1+0j)'
>>> f"{1.:}"  # default formatting slightly different than for floats
'1.0'

Unfortunately, formatting rules for complexes aren't documented in the Format Specification Mini-Language section. I'll work on a patch.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    docsDocumentation in the Doc dir

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions