forked from zhanghe06/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_decimal.py
More file actions
58 lines (46 loc) · 1.45 KB
/
test_decimal.py
File metadata and controls
58 lines (46 loc) · 1.45 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
#!/usr/bin/env python
# encoding: utf-8
"""
@author: zhanghe
@software: PyCharm
@file: test_decimal.py
@time: 16-6-2 下午2:56
"""
from decimal import Decimal
import json
# float(浮点型[二进制]) 转 decimal(十进制)
# float 先转 str 再转 decimal
print Decimal('0.12'), repr(Decimal('0.12'))
print str(Decimal('0.12')), repr(str(Decimal('0.12')))
print Decimal(0.00), repr(Decimal(0.00))
print Decimal(0.12), repr(Decimal(0.12))
print Decimal(0.12)+Decimal(0.36), repr(Decimal(0.12)+Decimal(0.36))
p_info = {
'p_name': 'a',
'p_price': Decimal('8.12')
}
print p_info
# print json.dumps(p_info) # raise TypeError(repr(o) + " is not JSON serializable")
def __default(obj):
"""
支持datetime的json encode
TypeError: datetime.datetime(2015, 10, 21, 8, 42, 54) is not JSON serializable
:param obj:
:return:
"""
if isinstance(obj, Decimal):
return str(obj)
else:
raise TypeError('%r is not JSON serializable' % repr(obj))
print json.dumps(p_info, default=__default)
"""
# 测试结果:
0.12 Decimal('0.12')
0.12 '0.12'
0 Decimal('0')
0.11999999999999999555910790149937383830547332763671875 Decimal('0.11999999999999999555910790149937383830547332763671875')
0.4799999999999999822364316060 Decimal('0.4799999999999999822364316060')
{'p_price': Decimal('8.12'), 'p_name': 'a'}
{"p_price": "8.12", "p_name": "a"}
# 订单系统必须采用 Decimal 类型存储, 避免浮点型误差累计产生的效应
"""