As an example I found a simple calculation like:
select cast(200.00 as float) + 1908.30 + 170.00 + (-1150.00) + (-1128.30)
As a normal addition this results in 0.00
but SQL Server shows the result as 2.27373675443232E-13
.
Why is this and how can I avoid this?
This error is inherent in the float
datatype, and is the reason for the existence of decimal
type. Never do money calculations as float
values!
You can take a look at What Every Computer Scientist Should Know About Floating-Point Arithmetic, or any Google result for "float rounding error".
select cast(200.00 as decimal(10,2)) + 1908.30 + 170.00 + (-1150.00) + (-1128.30)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With