Home > Back-end >  calculate with decimal values
calculate with decimal values

Time:10-14

At the the moment, I stored values in my mysql database as a decimal value (199,54). But If I get this value with php (mysql query) and would like to calculate with it:

echo ($row->myValue) / 5;

I get the error:

A non well formed numeric value encountered ...

Where is my mistake?

CodePudding user response:

You can use floatval function to convert decimal value that is fetched from database and then you can use in calculation:

$floatValue =  floatval($row->myValue);

Now you can use $floatValue variable in any calculation.

CodePudding user response:

As others have pointed out decimal separator in php is dot (.) not comma (,). Either change the separator on the DB level to . or run the following anytime you want to deal with numbers stored in the database. First two lines are just an example to get it running locally for me

<?php
    $row = new stdClass(); 
    $row->myValue = "15,29";    
    $myValue = str_replace(',', '.', $row->myValue) / 5;
    echo $myValue;
?>
  • Related