0

I am having a strange problem with MSSQL server. I am summing multiple numbers as float and decimal but since the numbers are the same, result must be the same. My number goes like "541,66", "700", "-1241,66". In this scenerio result must be "0", but when i sum numbers in float the result is different. You can see the details at the picture below.

different results

PS: When i change the numbers to the "541,67" and "-1241,67" both function is calculating correctly. As far as i can see it is only happening with ",66".

Any ideas?

Cromwell
  • 496
  • 1
  • 6
  • 24
  • 1
    Please check [Avg of float inconsistency](http://stackoverflow.com/questions/33322778/avg-of-float-inconsistency) and accurate vs inaccuarate datatypes. Number `-2.27 * 10e-13` is very close to 0. The point is that [float is imprecise](http://stackoverflow.com/questions/588004/is-floating-point-math-broken) – Lukasz Szozda Mar 09 '16 at 07:48
  • 1
    This might help also https://connect.microsoft.com/SQLServer/feedback/details/465147/select-sum-is-non-deterministic-when-adding-the-column-values-of-datatype-float when you add many float numbers. – Lukasz Szozda Mar 09 '16 at 07:49
  • @lad2025 thank you. i didnt tought that, this would be a general problem. i assumed that it is SQL only. i will look it up but as far as i can see only way to prevent this is convert my datatype to decimal right? – Cromwell Mar 09 '16 at 16:36
  • I would not call that a problem. It is by design. For money calculation or where precision is needed you cannot rely on floating point numbers. You should use fixed datatypes like `DECIMAL/NUMERIC` – Lukasz Szozda Mar 09 '16 at 16:38

0 Answers0