Search code examples
phpstr-replacenumber-formatting

"Notice: A non well formed numeric value encountered" PHP


I am getting the following error as a result of a calculation. I couldn't see any problem with the exit code. So why am I getting this error and how can I fix it?

// b2Cost = 19.020843;

return (float)number_format((float)str_replace(",", ".", $b2Cost), 2, ".", "");

Error:

Notice: A non well formed numeric value encountered 

Solution

  • Try

    return (float)number_format((float)str_replace(",", ".", (string) $b2Cost), 2, ".", "");
    

    But if you just want to shorten the float, take a look at https://www.php.net/manual/en/function.round.php