Skip to content

Commit 7a335ed

Browse files
committed
decimal docs: specification link and examples
* added a direct reference to a floating-point model used * and few examples for formatted string output
1 parent c141748 commit 7a335ed

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

Doc/library/decimal.rst

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,12 @@ The :mod:`decimal` module provides support for fast correctly rounded
3434
decimal floating-point arithmetic. It offers several advantages over the
3535
:class:`float` datatype:
3636

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

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

243+
Decimals could be formatted in fixed-point or scientific notation, using same
244+
formatting syntax (see :ref:`formatspec`) as builtin :class:`float` type:
245+
246+
.. doctest::
247+
248+
>>> format(Decimal('2.675'), "f")
249+
'2.675'
250+
>>> format(Decimal('2.675'), ".2f")
251+
'2.68'
252+
>>> format(Decimal('2.675'), ".2e")
253+
'2.68e+0'
254+
>>> with localcontext() as ctx:
255+
... ctx.rounding = ROUND_DOWN
256+
... print(format(Decimal('2.675'), ".2f"))
257+
...
258+
2.67
259+
241260
And some mathematical functions are also available to Decimal:
242261

243262
>>> getcontext().prec = 28

0 commit comments

Comments
 (0)