From: Johan De Meersman
Date: February 15 2013 10:43am
Subject: Re: MySQL 5.1: incorrect arithmetic calculation
List-Archive: http://lists.mysql.com/mysql/228978
Message-Id: <32600549.4847.1360925037570.JavaMail.root@tuxera.be>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
----- Original Message -----
> From: "Alex Keda"
>
> mysql> SELECT SUM(`Amount`*`Cost`*(1-`Discont`)) as `Summ` FROM `WorksCompliteAgregate` WHERE (`ContractID` = 10369 AND `Month` = 497);
Based off the select you printed, this comes to EXACTLY 548.595 for the first row and 0 for the second row.
> mysql> SELECT SUM(`Amount`*`Cost`*(1-`Discont`)*100)/100 as `Summ` FROM `WorksCompliteAgregate` WHERE (`ContractID` = 10369 AND `Month` = 497);
The more detailed result here, though, seems to suggest that there's a longer fraction in your table than is printed by your select. Would your column happen to be a Float?
> but, my desktop calculator gives the result 548.60
Which is the correct rounding for 548.595. Check if your column is a float, and if it is, go google for floating point mathematics. They do not work the way you think they do. Use decimal(n,m) for money - or any discrete number, for that matter. Floats are not exact values, they are APPROXIMATE values.
https://dev.mysql.com/doc/refman/5.0/en/problems-with-float.html may also give you an idea of what goes wrong.
--
Unhappiness is discouraged and will be corrected with kitten pictures.