2013-01-14 49 views
0

我有一個小問題來獲得正確的發票總額。01價值對總金額的差異

我對SQL表

unit_price decimal(25,2) 
vat varchar(55) 
total_novat decimal(25,2) 
total_vat decimal(25,2) 
grand_total decimal(25,2) 

php腳本來獲取值是:

$discount = 2 
$total_novat += qty * unit_price/discount 
$total_vat = qty * vat/100 
$grand_total = total_novat + total_vat 

好,現在如果我做這個簡單的腳本上值的結果是:

unit_price: 1 * 241.14/2 = 120.57 
total_novat: 120.57 * 24/100 = 28.94 
grand_total: 120.57 + 28.94 = 149.51 

問題是,當我把這個代碼在php中的結果是149.50而不是149.51

任何幫助表示讚賞。

完全腳本來執行計算:在PHP腳本

<?php 
$inv_total_no_tax = 0; 

       for($i=1; $i<=TOTAL_ROWS; $i++){ 
        if($this->input->post($quantity.$i) && $this->input->post($product.$i) && $this->input->post($unit_price.$i) && $this->input->post($discount.$i)) { 

         $tax_id = $this->input->post($tax_rate.$i); 
         $tax_details = $this->sales_model->getTaxRateByID($tax_id); 
         $taxRate = $tax_details->rate; 
         $taxType = $tax_details->type; 
         $tax_rate_id[] = $tax_id;    

         $inv_quantity[] = $this->input->post($quantity.$i); 
         $inv_product_code[] = $this->input->post($product.$i); 
         $inv_unit_price[] = $this->input->post($unit_price.$i)/$this->input->post($discount.$i); 
         $inv_unit_discount[] = $this->input->post($discount.$i); 
         $inv_gross_total[] = (($this->input->post($quantity.$i)) * ($this->input->post($unit_price.$i))/($this->input->post($discount.$i))); 

         if($taxType = 1) { 
         $val_tax[] = (($this->input->post($quantity.$i)) * ($this->input->post($unit_price.$i))/($this->input->post($discount.$i)) * $taxRate/100); 
         } else { 
         $val_tax[] = $taxRate; 
         } 

         if($taxType = 1) { $tax[] = $taxRate."%"; } else { $tax[] = $taxRate; } 

         $inv_total_no_tax += (($this->input->post($quantity.$i)) * ($this->input->post($unit_price.$i))/($this->input->post($discount.$i))); 

        } 
       } 

       $total_tax = array_sum($val_tax); 
       $total = $inv_total_no_tax + $total_tax; 

回合選項我改變了小數的MySQL爲varchar。

if i use $total = round($inv_total_no_tax + $total_tax, 2); the result is: 149.5 
if i use $total = round($inv_total_no_tax + $total_tax, 3); the result is: 149.501 

解決方案:

之前將其引入到mysql我已經申請此字符串。

number_format($value, 2, null, ''); 
+1

因爲您正在處理浮點數。谷歌對於「浮點錯誤」來看看很多話題。 – zerkms

+0

你是如何處理舍入數字的? 'total_noval = 28.9368' – datasage

+2

[每個程序員應該知道什麼關於浮點算術或 爲什麼我的數字不會累加?](http://floating-point-gui.de/) – 2013-01-14 20:34:50

回答

0

而不是十進制(25,2)嘗試十進制(25,3),然後在完成所有計算後四捨五入您的數字。這將使你的小數更準確。通常最好存儲儘可能準確的數字,然後在完成所有計算後對其進行格式化。

+0

如果我改變小數爲3分貝將顯示我到處的3個十進制結果,即使在最後0,我需要2個十進制。結果是正確的,只有總和是不正確的。因爲120.57 + 28.94 = 149.50,我不明白爲什麼。 – Dar

+0

@Dario:你正在執行計算?給出重現問題的完整腳本。 – zerkms

+0

是的,這就是爲什麼我說你在最後的計算後將其舍入。所以,在顯示號碼之前,只需要做number_format($ grand_total,2); – Kyle

2

的問題是如果是不四捨五入它看起來就像這樣(這是在PHP代碼中發生的事情)處理你在你的計算實例

unit_price: 1 * 241.14/2 = 120.57 
total_novat: 120.57 * 24/100 = 28.94 
grand_total: 120.57 + 28.94 = 149.51 

提出一個假設:

unit_price: 1 * 241.14/2 = 120.57 
total_novat: 120.57 * 24/100 = 28.9368 
grand_total: 120.57 + 28.9368 = 149.5068 

如果您將此值149.5068插入到mysql中,它將被存儲爲149.50(如果存儲在十進制列中,mysql將截斷,而不是整數)。相反,您需要對最終計算進行四捨五入,以使最終值爲149.51

+0

確切地說,我用number_format($ value,2,null,'')修復了這個問題;在將它引入到mysql表之前。謝謝分配 – Dar