I’m adding localisation to currencies, dates, and numbers in a project I’m continuously developing. However, when added number formatting using PHP’s NumberFormatter class, I get Warning: A non-numeric value encountered in....
This supposedly means that a non-numeric value was stored in my database. I’m not sure how this is possible since the respective column is decimal(11,20) but there’s no other explanation I can find. I must note that the quantity column used to be VARCHAR until a few years ago, when I switched it to DECIMAL, but I haven’t seen any errors or warnings before this.
Here’s the PHP code that throws the error:
$num = new NumberFormatter('en_UK', NumberFormatter::DECIMAL);
...
$units_quantity = $num->format($units_row['units_sum']);
...
if(isset($units_total[$year][$units_unit])) {
$units_total[$year][$units_unit] += $units_quantity; /* THIS LINE */
}
else {
$units_total[$year][$units_unit] = $units_quantity;
}
If I amend the PHP code above by casting the number as floatval like so:
$units_total[$year][$units_unit] += floatval($units_quantity);
I still get two non-numeric value warnings – but now the calculations are incorrect. There are a total of thirteen warnings before the change.
To try and find the non-numeric value in MySQLL, I tried these queries, both returning zero rows:
SELECT projectID, projectName, quantity FROM project_data WHERE quantity REGEXP '[a-zA-Z]';
SELECT projectID, projectName, quantity FROM project_data WHERE quantity REGEXP '^[^0-9]+$';
I hope you can point me in the right direction. Let me know if I can provide any additional details.