-
Notifications
You must be signed in to change notification settings - Fork 307
/
Copy pathlearn-decimal.py
63 lines (37 loc) · 1.1 KB
/
learn-decimal.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 如何利用decimal库来处理小数点问题
# 学习如何使用decimal库来处理小数点
# 文档地址: https://docs.python.org/zh-cn/3/library/decimal.html
value1 = 0.0000223233
print(value1)
value2 =str("0.0000223233")
print(value2)
## 四则运算
from decimal import Decimal, ROUND_UP, ROUND_DOWN
a = Decimal(10)
print(a, type(a))
b = Decimal(10.12) # 二进制方式存储的
print(b)
c = Decimal(str(10.12))
print(c)
#
# # 注意事项: Decimal对象不能跟其他进行数字类型进行运算,要把它们转成同类型的数据
#
# c1 = c + 11.2
# print(c1)
c1 = c + Decimal(str(11.2))
print(c1)
## 价格精度的问题
# btc, 38450.1, 38450.1234, 2500.12
price1 = Decimal(str(38450.15676))
d = price1.quantize(Decimal("0.1"))
print(d)
price2 = Decimal(str(2560.24567))
price2 = price2.quantize(Decimal("0.01"))
print(price2)
## round_up, round_down
price3 = Decimal(str(2560.24867))
price3 = price3.quantize(Decimal("0.01"), rounding=ROUND_DOWN)
print(price3)
price4 = Decimal(str(2560.241224))
price4 = price4.quantize(Decimal("0.01"), rounding=ROUND_UP)
print(price4)