2016-09-06 43 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) 
?>