简体   繁体   中英

PHP calculate with special chars

I'm working on a function that's getting data from a MySQL database and then it should calculate it. I've got the function like I want it, but my only problem is that my value in the database looks like:

€12.345,67

But PHP thinks the dots are commas. So with the formatting my result looks like:

25.000,00 + 40.000,00 =65.00

How it should look like:

25.000,00 + 40.000,00 = 65.000,00

I've already tried following parameter:

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

It didn't work, so I'm asking if someone has a clue how to do this.

My code:

<?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;?>

Try it:

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

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

Try to use Number Formatter Class like,

<?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) 
?> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM