当总结一组数字时,有时我会得到一些低的小数?为什么在数字被解析为字符串时会发生这种情况?我知道有一些% “&!大约漂浮数字总和返回奇数小数?
function parse(){
foreach($_SESSION['import_csv_posts']['result']['csv'] as $key => $post){
$amount = $this->parse_amount($post[$this->param['amount']]);
if($this->param['vat_amount']){
$amount += $this->parse_amount($post[$this->param['vat_amount']]);
}
$this->balance += $amount;
echo "$amount\n";
}
echo "\nbalance = ".$this->balance;
}
function parse_amount($amount){
$amount = strval($amount);
if(strstr($amount, '.') && strstr($amount, ',')){
preg_match('/^\-?\d+([\.,]{1})/', $amount, $match);
$amount = str_replace($match[1], '', $amount);
}
return str_replace(',', '.', $amount);
}
结果
-87329.00
-257700.00
-11400.00
-9120.00
-47485.00
-15504.00
122800.00
1836.00
1254.00
200.00
360.00
31680.00
361.60
1979.20
1144.00
7520.00
6249.49
balance = -399.00000000003
什么是“低位小数”? – deceze 2012-04-12 07:31:43
好吧,原谅我的无知,但我没有看到你在哪里解析字符串以获得浮点值? – Adi 2012-04-12 07:38:26
@adnan使用'+ ='时自动发生。 – deceze 2012-04-12 07:43:13