Skip to content

Document the behaviour of Decimal.__round__ #101575

@mdickinson

Description

@mdickinson

Documentation

The documentation is missing a description of the behaviour of round on Decimal objects. It's worth documenting, since the behaviour (particularly with respect to the context) is non-obvious.

Of particular note:

  • two-argument round respects the rounding mode from the Decimal context.
  • single-argument round always does round-ties-to-even, ignoring information from the context
Python 3.12.0a4 (main, Jan 20 2023, 16:04:05) [Clang 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from decimal import Decimal, getcontext, ROUND_DOWN
>>> getcontext().rounding = ROUND_DOWN
>>> round(Decimal('3.7'))  # round-ties-to-even; context rounding ignored
4
>>> round(Decimal('3.7'), 0)  # uses the context rounding
Decimal('3')

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