我有一个执行大量计算的存储过程,将结果存储在几个临时表中。最后计算总和并舍入到小数点后两位并存储在临时表中并选择它。
所有中间和最终临时表都具有数据类型 float 用于关注的列。
原始场景:
Declare @Intermediate table
{
--several other columns
Labor float
--several other columns
};
---Lots of calculation ---xx-----
Declare @Final table
{
--several other columns
LaborTotal float
--several other columns
};
INSERT INTO @Final SELECT ROUND(ISNULL((SELECT SUM([Labor]) FROM @Intermediate ),0),2) AS LaborTotal;
SELECT * FROM @Final;
Result: 7585.22 --> when rounded //Here is the error Expecting 7585.23
7585.225 --> when not rounded
测试用例 :
DECLARE @test float = 7585.225;
SELECT ROUND(@test,2) AS Result; --> results 7585.23
SELECT ROUND(7585.225,2) AS Result --> results 7585.23
将单个值插入临时表,然后计算总和
DECLARE @TmpTable table
(
MaterialAmount float
,LaborAmount float
);
INSERT INTO @TmpTable VALUES (12.10,1218.75);
INSERT INTO @TmpTable VALUES (12.10,1090.125);
INSERT INTO @TmpTable VALUES (12.10,900);
INSERT INTO @TmpTable VALUES (12.10,1632.6);
INSERT INTO @TmpTable VALUES (12.10,1625);
INSERT INTO @TmpTable VALUES (12.10,1118.75);
SELECT ROUND(ISNULL((SELECT SUM(MaterialAmount) FROM @TmpTable), 0),2) AS MatSum,
ISNULL((SELECT SUM(LaborAmount) FROM @TmpTable), 0) AS LabSumUnrounded, --> 7585.225
ROUND(ISNULL((SELECT SUM(LaborAmount) FROM @TmpTable), 0),2) AS LabSum; --> 7585.23
SELECT ROUND(SUM(MaterialAmount),2),
ROUND(SUM(LaborAmount),2) ---> 7585.23
FROM @TmpTable;
任何想法/建议为什么我在原始场景中得到 0.01 的差异,同时在所有测试用例中获得准确的值?提前致谢。