skip to Main Content

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?

2

Answers


  1. 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.

    Login or Signup to reply.
  2. 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;
    ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search