Even though DECIMAL
is an exact numeric type (unlike FLOAT
, which is approximate), it behaves rather strangely in the following example:
DECLARE @DECIMAL_VALUE1 DECIMAL(20,9) = 504.70 / 0.151562
DECLARE @DECIMAL_VALUE2 DECIMAL(20,0) = 504.70 / 0.151562
DECLARE @INTEGER_VALUE INT = 504.70 / 0.151562
SELECT
@DECIMAL_VALUE1 AS DECIMAL_VALUE1, -- 3329.990366978
@DECIMAL_VALUE2 AS DECIMAL_VALUE2, -- 3330
@INTEGER_VALUE AS INTEGER_VALUE -- 3329
A value other than 3329 causes a bug in our application. Making the variable type an INTEGER
solved our issue, but I cannot get my head around as to why it was caused in the first place.