适用于有高精度需求的场景

导入:decimal模块

from decimal import Decimal

class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, Decimal):
return str(obj)
return json.JSONEncoder.default(self, obj)

val = Decimal(1)/Decimal(3)
number = val.quantize(Decimal("0.00000000")) #0.33333333
result = {'data': Decimal(number)}
return json.dumps(result, cls=DecimalEncoder)

标签: none

评论已关闭