Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 23 additions & 4 deletions Doc/library/decimal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ The :mod:`decimal` module provides support for fast correctly rounded
decimal floating-point arithmetic. It offers several advantages over the
:class:`float` datatype:

* Decimal "is based on a floating-point model which was designed with people
in mind, and necessarily has a paramount guiding principle -- computers must
provide an arithmetic that works in the same way as the arithmetic that
people learn at school." -- excerpt from the decimal arithmetic specification.
* Decimal "is based on a [floating-point
model](https://speleotrove.com/decimal/damodel.html#refnumber) which was
designed with people in mind, and necessarily has a paramount guiding
principle -- computers must provide an arithmetic that works in the same way
as the arithmetic that people learn at school." -- excerpt from the decimal
arithmetic specification.

* Decimal numbers can be represented exactly. In contrast, numbers like
``1.1`` and ``2.2`` do not have exact representations in binary
Expand Down Expand Up @@ -238,6 +240,23 @@ floating-point flying circus:
>>> c % a
Decimal('0.77')

Decimals could be formatted in fixed-point or scientific notation, using same
formatting syntax (see :ref:`formatspec`) as builtin :class:`float` type:

.. doctest::

>>> format(Decimal('2.675'), "f")
'2.675'
>>> format(Decimal('2.675'), ".2f")
'2.68'
>>> format(Decimal('2.675'), ".2e")
'2.68e+0'
>>> with localcontext() as ctx:
... ctx.rounding = ROUND_DOWN
... print(format(Decimal('2.675'), ".2f"))
...
2.67

And some mathematical functions are also available to Decimal:

>>> getcontext().prec = 28
Expand Down
Loading