I am fetching (with PHP PDO) from MySQL a value (stored in signed Float(7,2) format).
Result
=====================
Database = 8.9 //correct value
PHP = 8.8999996185303 //fetch from PHP PDO
Sample code:
$qry = 'SELECT hours FROM sample WHERE filter=?;';
$stmt = $db->prepare($qry);
$stmt->execute(array($filter));
while($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['hours'];
}
What is the best possible way to fetch this data from the DB? Ive read about the floating point approximalization issue in PHP. Thus I was thinking to perhaps change the SQL into something like:
SELECT (hours * 100) as butSoExpensiveOnDatabase etc...
Or using round
or number_format
in PHP. What would you guys suggest and why?
Changing the table structure is NOT an option!
(reason: old legacy code I need to support + no time + no budget)
You can do this
"SELECT round(hours, 2) FROM sample WHERE filter=?;"