This is a problem with floating point numbers. This is a design feature, not a drawback.
Verify that the value returned from the database is not a floating point value, but a string or decimal. (If the data types `price` and` count` are both DECIMAL, then the resulting expression must be DECIMAL.
If any of them is a floating point, you can convert it to DECIMAL ...
SELECT brand, CONVERT( SUM(count * price) / SUM(count), DECIMAL(18,2) ) WHERE ... GROUP BY brand, ...;
Or convert to string
SELECT brand, CONVERT(CONVERT( SUM(count * price) / SUM(count), DECIMAL(18,2)),CHAR) WHERE ... GROUP BY brand, ...;
You can let the DECIMAL conversion do rounding for you. If you return DECIMAL or VARHCAR to Perl, this should prevent floating point problems.
More generally, to handle a floating point representation (rounding) in Perl, you can format it using the sprintf function, for example.
my $rounded_val = sprintf(%.2f, $float_val);
source share