【发布时间】:2016-03-01 17:41:28
【问题描述】:
我正在尝试在 php 中自动计算税金。这是我到目前为止所拥有的:
<?php
// Formats a number so it appears as such : 55.12, instead of 55.12354
function invoiceNumFormat($number){
return number_format($number, 2);
}
$tax_rate = 7.5; //Sets Tax Rate
// Retrieves Subtotal, utilitzes NumFormat function to round to hundredths place
$invoice_subtotal = isset($invoice['invoice_subtotal']) ? invoiceNumFormat( $invoice['invoice_subtotal'] ): 0;
//Multiplies subtotal against tax rate to retrieve $tax_amount
$tax_amount = invoiceNumFormat( $invoice_subtotal*$tax_rate/100 );
// Shows Grand Total(subtotal + tax)
$invoice_totalled = $invoice_subtotal + $tax_amount;
//Displays Totals
<span>'.$invoice_subtotal.'</span>
<span>'.$tax_amount.'</span>
<span>'.$invoice_totalled.'</span>
?>
我有一个显示“3116.88”的小计输出,这是正确的,但该金额的税显示为“.32”,总计为“3.23”。谁能告诉我哪里出错了?
【问题讨论】:
-
我用 $invoice_subtotal = 3116.88 试过你的代码; //硬编码值,结果很好,总计:3116.88,税:233.77,总计:3350.65。所以我认为,$invoice_subtotal 变量下有一些错误的值。
-
@ameenulla0007 进行了更多故障排除,每当我取出 invoiceNumFormat 函数时,发现所有总数都很好。现在的问题是,我仍然需要将其四舍五入到美元金额......
-
美元金额!!你能说清楚吗..
标签: php