所以我不能弄清楚这里有什么问题。我正试图将销售税计算为总计。我想在这里得到两个变量:PHP销售税计算
A. The total amount of sales tax that is being charged (i.e. $0.61 for sales tax)
B. The Grand total including the item price times the quantity plus the sales tax.
我的脚本如下,顺便我也该设置现在它只会增加一分钱给分总。 (7.00 $总额反而是$ 7.01)
如果每个项目后,子总数为$7.00
,税率为8.650%
那么税收总应该是$0.61
和总计应$7.61
但它使它$7.01
代替。
public function invoice_totals($invoice_id)
{
$query = $this->CI->db->select('*');
$query = $this->CI->db->from('invoice_to_items');
$query = $this->CI->db->where('invoice_id', $invoice_id);
$query = $this->CI->db->join('items', 'items.item_id = invoice_to_items.item_id');
$query = $this->CI->db->get();
$items = $query->result();
$sub_total = '0.00';
$grand_total = '0.00';
$tax_rate = '0.0865';
$tax_total = '0.00';
foreach($items as $item)
{
$sub_total = $sub_total + ($item->item_price*$item->item_qty);
$tax_total = $tax_total + ($sub_total * $tax_rate)/100;
}
$grand_total = $grand_total + $sub_total + $tax_total;
return array(
'sub_total' => number_format($sub_total,2),
'tax_total' => number_format($tax_total, 2),
'grand_total' => number_format($grand_total,2),
);
}
我这个问题,我关心的主线是:
$tax_total = $tax_total + ($sub_total * $tax_rate)/100;
100卸下师,你的$ TAX_RATE已经是十进制形式。另外,用数字而不是字符串初始化你的变量。 – 2014-08-29 20:32:58
那么你应该可能不应该实现的东西,可能大概甚至会有法律影响,如果做错了... – CBroe 2014-08-29 20:40:08
@ user3968645我很抱歉告诉你这个,但编程和数学齐头并进。如果你不能进行基本的数学运算,那么作为一名程序员你的未来就会显得暗淡无光。 – 2014-08-29 20:41:34