forked from pynutshell/pynut4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decimal_module.py
42 lines (36 loc) · 952 Bytes
/
decimal_module.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
"""
>>> from decimal import Decimal
>>> df = Decimal(0.1)
>>> df
Decimal('0.1000000000000000055511151231257827021181583404541015625')
>>> ds = Decimal(str(0.1)) # or, directly, Decimal('0.1')
>>> ds
Decimal('0.1')
def dfs(x):
return Decimal(str(x))
>>> dq = Decimal(0.1).quantize(Decimal('.00'))
>>> dq
Decimal('0.10')
>>> pidigits = (3, 1, 4, 1, 5)
>>> Decimal((1, pidigits, -4))
Decimal('-3.1415')
>>> import math
>>> a = 1.1
>>> d = Decimal('1.1')
>>> a == d
False
>>> math.isclose(a, d)
True
>>> a + d
Traceback (most recent call last):
...
TypeError: unsupported operand type(s) for +: 'float' and 'decimal.Decimal'
>>> d + Decimal(a) # new decimal constructed from a
Decimal('2.200000000000000088817841970')
>>> d + Decimal(str(a)) # convert a to decimal with str(a)
Decimal('2.2')
"""
if __name__ == '__main__':
# use doctest to simulate console sessions
import doctest
doctest.testmod(verbose=True, exclude_empty=True)