0

請求書の正しい合計を取得するのに小さな問題があります。

私は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.51ではなく149.50になることです。

どんな助けでも大歓迎です。

計算を実行するための完全なスクリプト:

<?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;

phpスクリプトのラウンドオプション私は10進数の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, '');
4

2 に答える 2

2

問題は、計算例で行った仮定にあります

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 コードで起こっていることです):

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 は、10 進数の列に格納されている場合、追加の桁を四捨五入するのではなく切り捨てます)。代わりに、最終的な値が次のようになるように、最終的な計算を丸める必要があります。149.51

于 2013-01-14T20:50:32.977 に答える
0

decimal(25,2) の代わりに decimal(25,3) を試し、すべての計算を行った後に数値を丸めます。これにより、小数がより正確になります。通常は、できるだけ正確な数値を保存し、すべての計算が完了した後にフォーマットすることをお勧めします。

于 2013-01-14T20:39:26.850 に答える