总金额之差为01值


Difference of 01 value on total sum

我在获取正确的发票总额方面遇到了一个小问题。

我有一个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
$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脚本中的四舍五入选项我将十进制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, '');

问题在于您在示例计算中所做的假设

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将截断,而不是四舍五入,如果存储在十进制列中)。相反,您需要将最终计算四舍五入,使最终值为149.51

代替decimal(25,2),尝试decimal(25,3),然后在完成所有计算后将数字四舍五入。这将使你的小数更精确。通常最好是存储尽可能精确的数字,然后在所有计算完成后对其进行格式化。