Skip to content

Removing inconsistency in number formatting. #129503

@slawomirmarczynski

Description

@slawomirmarczynski

There is a discrepancy between the default float formatting achieved in different ways, see the source code snippet. It would be appropriate if using :f would by default give the same effect to a float as printing that number without using :f. Currently this is not the case, using :f unexpectedly changes the number of digits printed after the decimal point. Unexpectedly, because it changes one default setting to a completely different default setting.

value = 1.123456789

print(value)                # print result is 1.123456789
print(f"{value}")           # print result is 1.123456789
print(f"{float(value)}")    # print result is 1.123456789
print(f"{value:f}")         # print result is 1.123457
print(f"{float(value):f}")  # print result is 1.123457

# However,
#
# "[...] With no precision given, uses a precision of 6 digits
#  after the decimal point for float [...]"
#
# https://docs.python.org/pl/3/library/string.html#formatstrings

Metadata

Metadata

Assignees

No one assigned

    Labels

    interpreter-core(Objects, Python, Grammar, and Parser dirs)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions