pymongo浮子精度

时间:2017-06-27 06:19:23

标签: python-2.7 pymongo floating-accuracy

我在mongo中存储的数字为15000.245263,小数点后有6个数字,但是当我使用pymongo得到这个数字时,我得到了15000.24。 pymongo是否降低了漂浮的精度?

2 个答案:

答案 0 :(得分:0)

我无法重现这一点。在我的Mac上的Python 2.7.13中:

>>> from pymongo import MongoClient
>>> c = MongoClient().my_db.my_collection
>>> c.delete_many({})  # Delete all documents
>>> c.insert_one({'x': 15000.245263})
>>> c.find_one()
{u'x': 15000.245263, u'_id': ObjectId('59525d32a08bff0800cc72bd')}

检索到的" x"打印时与我输入时的打印方式相同。

答案 1 :(得分:0)

如果您尝试打印出长浮点值,可能会发生这种情况,我认为它与mongodb无关。

>>> print 1111.1111
1111.1111

>>> print 1111111111.111
1111111111.11

>>> print 1111111.11111111111
1111111.11111

# for a timestamp
>>> import time
>>> now = time.time()
>>> print now
1527160240.06

对于python2.7.10,它只显示13个字符(对于我的机器),如果要显示整个值,请使用格式,如下所示:

>>> print '%.6f' % 111111111.111111
111111111.111111

这只是一个显示问题,变量的值不会受到影响。

>>> test = 111111111.111111 * 2
>>> test
222222222.222222
>>> print test
222222222.222
相关问题