2013-12-10 56 views
0
$invoiceTotal = $settings['Setting']['currency'] . $thisInvoice['Invoice']['invoice_total']; 

$bedrag = $settings['Setting']['currency'] . number_format($thisInvoice['Payment']['0']['amount'], 2, '.', ','); 

$openbedrag = $invoiceTotal - $bedrag; 

//$invoiceTotal = 2 
//$bedrag = 1 

爲什麼$openbedrag = $invoiceTotal - $bedrag;無法正常工作?無法計算值

這是一個簡單的計算,爲什麼它不計算這個變量?

它輸出0 -_-」

UPDATE問題修正

$invoiceTotal = $settings['Setting']['currency'].$thisInvoice['Invoice']['invoice_total']; 
$bedrag = $settings['Setting']['currency'].number_format($thisInvoice['Payment']['0']['amount'], 2, '.', ','); 
$openbedrag2 = $thisInvoice['Invoice']['invoice_total'] - $thisInvoice['Payment']['0']['amount']; 
$openbedrag = $settings['Setting']['currency'].$openbedrag2; 

貨幣設置€

+4

當數字以貨幣符號開頭時,您無法進行算術運算。 – Barmar

+0

什麼$設置['設置'] ['貨幣']這部分包含?價值或符號? – ripa

+0

@ripa它大概包含一個貨幣符號,例如美元爲'$'或英鎊爲''''。 – Barmar

回答

1

你需要做的算術與原始數字,而不是格式化 那些。

$openbedrag = $thisInvoice['Invoice']['invoice_total'] - $thisInvoice['Payment']['0']['amount']; 
+0

感謝,愚蠢的我。在數字之前忘了'$ settings ['Setting'] ['currency']' - ' - ' – user3078542

0

轉換成整數: -

$openbedrag = ($invoiceTotal * 1.0) - ($bedrag * 1.0); 
+0

相同的輸出顯示爲0 – user3078542

+0

當您使用算術運算符時,PHP會自動將字符串轉換爲整數。 – Barmar

+0

@ user3078542:請確保您的貨幣值沒有貨幣符號 – Suleman

0

如果$設置[ '環境'] [ '貨幣']這種含符號,儘量避免使用$設置[ '環境'] [ '貨幣']在你的計算時間。所以你的計算會像下面: -

$invoiceTotal = $thisInvoice['Invoice']['invoice_total']; 

$bedrag = $thisInvoice['Payment']['0']['amount']; 

$openbedrag = $invoiceTotal - $bedrag; 

$openbedrag = $settings['Setting']['currency'].number_format($openbedrag, 2, '.', ','); 

注意: - 也不要在你的計算時間使用number_format。使用它在最終輸出中計算後顯示時間。