2016-09-06 44 views
0

我正在從一個MySQL數據庫獲取數據的函數,然後它應該計算它。我已經得到了功能像我想的那樣,但我的唯一的問題是,我在數據庫中值的樣子:PHP與特殊字符計算

€12.345,67

但是PHP認爲點是逗號。因此,與格式化我的結果是這樣的:

25.000,00 + 40.000,00 = 65.00

如何它應該看起來像:

25.000,00 + 40.000,00 = 65.000,00

我已經試過以下參數:

str_replace(',', '.', $value1); 

它沒有工作,所以我問如果有人有線索如何做到這一點。

我的代碼:

<?php 

$con=mysqli_connect("localhost","root","pass","DB-Nane"); 
// Check connection 
if (mysqli_connect_errno()) 
{ 
echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
} 

$result = mysqli_query($con,"SELECT * FROM Fahrzeugverkauf WHERE Fahrzeugkonto = 2"); 
$rahmen = 230000; 
while($row=mysqli_fetch_array($result)){ 
    $total3 = $row['EKBrutto']; 
    $test +=$total3; 
    $belastung= $rahmen - $test; 


} 
//$res1 = 

mysqli_close($con); 

?> 

<?php echo "Verfügbarer Rahmen: " .$rahmen;?>  <br> 
<?php echo "Belastung: " .$test;?>  <br> 
    <?php echo "Verbliebenes Guthaben: " .$belastung;?> 
+0

你試過到u你可以使用'number_format()'函數嗎? – SuperDJ

+0

還沒有,我會試試看! –

+0

嘗試它: http://php.net/manual/es/function.money-format.php – Vitorlui

回答

0

試試:

setlocale(LC_MONETARY, 'en_US'); 
echo money_format('%(#10n', $number) . "\n"; 
// ($  1,234.57) 

來自: http://php.net/manual/es/function.money-format.php

+0

這是有點工作,我唯一的問題是我怎麼可以在同一時間做逗號和點? –

+0

如果你先刪除所有的點,並在點替換逗號後,它也可以工作:str_replace(',','。',str_replace('。','',$ value1)); – Vitorlui

+1

但我仍然認爲如果你在你的數據庫使用貨幣類型,你應該使用money_format或其他函數來處理貨幣數據格式......如果你的數據庫是varchar,你應該改變爲貨幣或至少某些數字類型作爲浮動。 – Vitorlui

0

嘗試使用Number Formatter Class一樣,

<?php 
    $a = new \NumberFormatter("it-IT", \NumberFormatter::CURRENCY); 
    echo $a->formatCurrency(12345, "USD") . "<br>"; // outputs $ 12.345,00 and it is formatted using the italian notation (comma as decimal separator) 
?>