关于Mysql计算出的数值和计算器算出的不一样怎么解决!

WBOY
Release: 2016-06-20 12:54:22
Original
1008 people have browsed it


如上图这个sql 。cjPrice是600 cjNum是5.97 这种情况下 

上面的sql算出的是3581.9998741149902 而计算器算出的是3582

而我用phpmyadmin执行如下sql 得到的也是3582 到底什么问题啊

select sum(600*5.97) 这也是3582 


请问到底怎么解决啊~~~


回复讨论(解决方案)

如果你的 cjNum 不是 DECIMAL 类型的,那么应用 round 取结果

字段类型用的是 float 吧
单精度不准确
对精度要求高的话,建议换成double、decimal、numeric
double双精度,效率上差一点,但准确性高
decimal和numeric几乎等价,必须指定具体精度

你的查询结果是跟你那相乘的两个字段类型有关的

cjPrice整型 cjNum Float型
在phpmysql 下执行,是与mysql计算一样的,有小数位的。

估计和版本有关系吧,
可以试试 select  round(SUM(cjPrice*cjNum), 2) from ..
保留两位小数

的确,我的cjPrice是decimal         cjNum是float 我把 cjNum也改成decimal就行了

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!