There is a database with a table with columns:

| quantity | price | discount | *(количество|цена|скидка) 

at recalculation of the sums taking into account a discount the sums turn out:

 mysql_query( "SELECT SUM(price * quantity) AS summ FROM custom_item WHERE id_custom='$zakaz'", $db); 12 | 189.00 | 0% | = 2268.00 12 | 153.12 | 0% | = 1837.44 24 | 110.89 | 15% | = 2262.24 

Everything works fine. But you need to calculate the amount of all orders, and I did this:

 mysql_query( "SELECT SUM( (price * quantity)-(price * quantity * (discount/100)) ) AS summ FROM custom_item WHERE id_custom='$zakaz'", $db ) 

That's just when recalculating on a calculator it turns out like this:

2268.00 + 1837.44 + 2262.24 = 6367.68 ,

and when counts mysql = 6367.5959 , I round by function

 number_format($myro2['0'], 2, '.', ''); 

and get 6367.60 . What's the matter, friends, how to solve the problem?

    2 answers 2

    Eh, school ... mathematics ... Everything is clear with the first two lines, but the third one:

     1) 24 * 110.89 = 2661.36 (сумма без скидки) 2) 2661.36 / 100 * 15 = 399.204 (скидка) 3) 2661.36 - 399.204 = 2262.156 (итоговая сумма) 

    Now summarize everything together:

     2268.00 + 1837.44 + 2262.156 = 6367.596 

    We start your request with almost no editing and see 6367.596 . And your calculator is necessary;)

    • Thanks for the detailed answer! Everything turned out to be much simpler than it seemed ... Often you assume a more complex solution, and the answers are also on the surface. - cheh1
    • @ cheh1, not at all. But I forgot to emphasize that for the " price " field, I used the type not float , but decimal . Although, if you watched an example , you probably noticed it. - Deonis

    It should not be used for monetary and quantitative values ​​of floating-point numbers. Use DECIMAL .