json.loads在python中将小数点转换为'e'

时间:2018-01-15 17:34:14

标签: python json python-3.x floating-point

以下是我从服务器获取的回复

[{"type":"bid","price":0.00000026,"amount":737.15054457,"tid":200001915,"timestamp":1516036570}]

我正在尝试使用

将此字符串解析为JSON
json_data = json.loads (req.text)

但是,当我尝试使用json_data[0]['price']阅读“价格”时,输出为2.6e-07

我尝试将数据解析为json_data = json.loads (req.text, parse_float=Decimal),但仍然没有区别。

2 个答案:

答案 0 :(得分:1)

这是python显示浮动的方式

price = 0.00000026
print(price)

输出:2.6e-07 如果你想看到它正常,你可以这样打印:

print('{0:.8f}'.format(price))

输出:0.00000026

答案 1 :(得分:0)

Your value is parsed as a Decimal, it's just shown in an exponential form because it's more compact:

>>> x = json.loads('{"a":0.00000000000000026}', parse_float=decimal.Decimal)
>>> repr(x)
"{'a': Decimal('2.6E-16')}"

You can see that the precision is preserved, though, unlike with a float:

>>> x['a'] + 1
Decimal('1.00000000000000026')
>>> 1 + 2.6e-16
1.0000000000000002

So everything works as expected.