7
I have this scenario: - multiply by 2 values which will be rounded to 2 boxes for the table to be inserted.
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, doing cast
in the second value for multiplication to the first account 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
.
So to solve the problem of the second account just simply take the cast
in the second value: 0.011500 * 50.00 = 0.575000
, rounding is OK, resulting in 0.58
. But on the first count 0.002071 * 84.50 = 0.175000
comes out with the wrong result and when we give the value 0.18
is OK.
Any idea what it is?
Try it like this
ROUND( CAST(A AS DECIMAL(10,2) * CAST(B AS DECIMAL(10,2), 2)
– Marcos Regis
@Marcosregis if I cast a value from A to 2 decimal places I will be ignoring value.
– revton