Python论坛  - 讨论区

标题:[python-chinese] MySQLdb对float字段的处理

2007年09月06日 星期四 09:57

leopay leopay在gmail.com
星期四 九月 6 09:57:49 HKT 2007

ij¸ö×Ö¶ÎÊÇfloatµÄ£¬±È·½Ëµ9.1£¬ÓÃMySQLdbÈ¡³öÀ´ÔõôÊÇ9.0999999999999996£¬
ÓÐûÓбȽϺõĽâ¾ö·½·¨£¿
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070906/1db31a91/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年09月06日 星期四 10:59

Leo Jay python.leojay在gmail.com
星期四 九月 6 10:59:21 HKT 2007

On 9/6/07, leopay <leopay在gmail.com> wrote:
> 某个字段是float的,比方说9.1,用MySQLdb取出来怎么是9.0999999999999996,
> 有没有比较好的解决方法?

这个跟mysql没关系,计算机不能精确的表示浮点数。其实这种1e-12以内的误差,
一般不会造成什么不良后果吧?
如果你想输出的话,可以这样啊:
>>> result = 9.0999999999999996
>>> result
9.0999999999999996
>>> print '%.4f' % result
9.1000
>>>

-- 
Best Regards,
Leo Jay

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号