-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecimalCoding.py
More file actions
executable file
·63 lines (47 loc) · 1.23 KB
/
DecimalCoding.py
File metadata and controls
executable file
·63 lines (47 loc) · 1.23 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Mar 25 13:17:08 2021
@author: maherme
"""
#%%
import decimal
from decimal import Decimal
print(decimal.getcontext())
# We can change values in the context
decimal.getcontext().prec = 6
print(decimal.getcontext())
#%%
# You can also change the context in this way:
g_ctx = decimal.getcontext()
g_ctx.rounding = decimal.ROUND_HALF_UP # In this way you avoid misspelling
print(decimal.getcontext())
#%%
# Restore the original values:
g_ctx.prec = 28
g_ctx.rounding = decimal.ROUND_HALF_EVEN
print(decimal.getcontext())
#%%
# Notice the difference between the localcontext and the getcontext type:
print(type(decimal.localcontext()))
print(type(decimal.getcontext()))
#%%
# Notice the localcontext is the same than getcontext:
with decimal.localcontext() as ctx:
ctx.prec = 6
ctx.rounding = decimal.ROUND_HALF_UP
print(ctx)
print(decimal.getcontext())
print(id(ctx) == id(decimal.getcontext()))
#%%
# Notice the difference between context:
x = Decimal('1.25')
y = Decimal('1.35')
with decimal.localcontext() as ctx:
ctx.prec = 6
ctx.rounding = decimal.ROUND_HALF_UP
print(round(x, 1))
print(round(y, 1))
print(round(x, 1))
print(round(y, 1))
#%%