I have this scenario: - do a multiplication of 2 values that will be rounded to 2 boxes for the table that will be inserted its result.
Follow the example:
CREATE TABLE #TMP (
A DECIMAL(23,6),
B DECIMAL(28,2)
)
INSERT INTO #TMP VALUES (0.002071, 84.50)
INSERT INTO #TMP VALUES (0.011500, 50.00)
SELECT *,
A * CAST(B AS FLOAT) res_cast,
ROUND(A * CAST(B AS FLOAT),2) res_cast_round,
A * B res,
ROUND(A * B,2) res_round
FROM #TMP
DROP TABLE #TMP
The results in SQL Server 2012 are:
As we can see, by doing cast
in the second value for the multiplication for the first count 0.002071 * 84.50 = 0.1749995
, the result and rounding in this case is OK, resulting in 0.17
. But in the second account 0.011500 * 50.00 = 0.575
, rounding is wrong, resulting in 0.57
.
Then, to solve the problem of the second account, simply remove the cast
from the second value: 0.011500 * 50.00 = 0.575000
, the rounding is OK, resulting in 0.58
. But in the first count 0.002071 * 84.50 = 0.175000
already comes out with the wrong result and when we round the 0.18
value is OK.
Any idea what it is?