PHP减法给出了错误的结果

时间:2017-04-12 18:47:13

标签: php

今天PHP声称​​ 30.26 - 30.26 = -3.5527136788005E-15

我的计算器(和常识)说它应该等于0.计算中使用的两个数字都来自MySQL数据库,数据类型是varchar(255)。考虑到floatval()这一点并不能解决问题。有谁知道这里发生了什么?

/*  this is what the table looks like...
__________________________________________
|uid    |AccountNumber|    Total|    Paid|
__________________________________________
|51     |3255         |    30.26|   30.26|
__________________________________________
*/

$q = "SELECT Total, Paid FROM Account WHERE AccountNumber='3255';";
$r = mysqli_query($db, $q);
$vars = mysqli_fetch_array($r);

$balance = floatval($vars['Total']) - floatval($vars['Paid']);
echo $balance;

输出

-3.552713678805E-15

0 个答案:

没有答案