@@ -34,10 +34,12 @@ The :mod:`decimal` module provides support for fast correctly rounded
3434decimal 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+
241260And some mathematical functions are also available to Decimal:
242261
243262 >>> getcontext().prec = 28
0 commit comments