請求書の正しい合計を取得するのに小さな問題があります。
私は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, '');