SQL Server舍入错误,给出不同的值
发布时间:2021-02-28 03:15:03 所属栏目:MsSql教程 来源:网络整理
导读:我有一个存储过程,它执行大量计算,将结果存储在几个临时表中. 最后计算总和并舍入到两位小数并存储在临时表中并选择它. 对于所关注的列,所有中间和最终临时表都具有数据类型float. 原始场景: Declare @Intermediate table{ --several other columnsLabor
我有一个存储过程,它执行大量计算,将结果存储在几个临时表中.
对于所关注的列,所有中间和最终临时表都具有数据类型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),2) AS MatSum,ISNULL((SELECT SUM(LaborAmount) FROM @TmpTable),0) AS LabSumUnrounded,--> 7585.225 ROUND(ISNULL((SELECT SUM(LaborAmount) FROM @TmpTable),2) AS LabSum; --> 7585.23 SELECT ROUND(SUM(MaterialAmount),2),ROUND(SUM(LaborAmount),2) ---> 7585.23 FROM @TmpTable; 任何想法/建议为什么我在我的原始场景中获得0.01差异,同时在我的所有测试用例中获得准确的值? 解决方法这是因为您使用的是float数据库类型.Float不应该用于表示需要精度的值,因为它们存储为近似值,不同的操作可以给出不同的结果. 在SQL Server中,您可以使用十进制和数字数据类型进行数值精度: (编辑:应用网_镇江站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- sql-server – 我有关于死锁的数据,但我无法理解它们为什么
- sql-server – 我可以在2008服务器上使用SQL Server Manage
- 万元杀毒版 500元贱卖 江民公司密谋价格战
- sql-server – 如何确定服务器上是否正在使用SQL Server ld
- 微软展示beta 2版Office 2003 内容不止四大件
- 微软官方:Office 2003将在10月21日发布
- windows storage server 2008创建iscsi磁盘
- sql – 将一行转换为具有较少列的多行
- sql-server-2008 – SSIS包中的数据转换问题 – 文本到GUID
- 《仙剑2》:一个单机版游戏的“典型性盗版尴尬”
站长推荐
热点阅读