Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-101575: document Decimal.__round__() #101737

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
38 changes: 38 additions & 0 deletions Doc/library/decimal.rst
Expand Up @@ -897,6 +897,44 @@ Decimal objects
:const:`Rounded`. If given, applies *rounding*; otherwise, uses the
rounding method in either the supplied *context* or the current context.

.. describe:: round(number, ndigits=None)

If *ndigits* is ``None``, returns the nearest :class:`int` to *number*,
rounding ties to even, and ignoring the rounding mode of the
:class:`Decimal` context. Raises :exc:`OverflowError` if *number* is an
infinity or :exc:`ValueError` if it is a (quiet or signaling) NaN.

If *ndigits* is an :class:`int`, the context's rounding mode is respected
and a :class:`Decimal` representing *number* rounded to the nearest
multiple of ``Decimal('1E-ndigits')`` is returned; in this case,
``round(number, ndigits)`` is equivalent to
``self.quantize(Decimal('1E-ndigits'))``. Returns ``Decimal('NaN')`` if
*number* is a quiet NaN. Raises :class:`InvalidOperation` if *number*
is an infinity, a signaling NaN, or if the length of the coefficient after
the quantize operation would be greater than the current context's
precision. In other words, for the non-corner cases:

* if *ndigits* is positive, return *number* rounded to *ndigits* decimal
places;
* if *ndigits* is zero, return *number* rounded to the nearest integer;
* if *ndigits* is negative, return *number* rounded to the nearest
multiple of ``10**abs(ndigits)``.

For example::

>>> from decimal import Decimal, getcontext, ROUND_DOWN
>>> getcontext().rounding = ROUND_DOWN
>>> round(Decimal('3.75')) # context rounding ignored
4
>>> round(Decimal('3.5')) # round-ties-to-even
4
>>> round(Decimal('3.75'), 0) # uses the context rounding
Decimal('3')
>>> round(Decimal('3.75'), 1)
Decimal('3.7')
>>> round(Decimal('3.75'), -1)
Decimal('0E+1')


.. _logical_operands_label:

Expand Down