Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rounded numbers returned as '0.999999999992345' sometimes

I have a report that should return something along the lines of

SELECT brand, ROUND(SUM(count * price) / SUM(count), 2) 
    WHERE ... GROUP BY brand, ...; 

The problem is, I sometimes get 9990.32999999999992345 in my perl code instead of 9990.33 which direct SQL request returns.

The number starts looking that way right after fetchrow_hashref, if it ever does. The same number can come in 'good' or 'bad' form in different queries, but always the same way in any specific query.

How can I track this down?

like image 755
Dallaylaen Avatar asked Nov 28 '22 03:11

Dallaylaen


1 Answers

Read all about floating point accuracy problems here: http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

like image 155
DanSingerman Avatar answered Nov 30 '22 16:11

DanSingerman