[英]SQL round off issue
Consider these values which are of type MONEY
(sample values and these can change) 考虑这些类型为MONEY
值(样本值,并且这些值可以更改)
select 4796.529 + 1585.0414 + 350.9863 + 223.3549 + 127.6314+479.6529 + 158.5041
for some reason I need to round each value to a scale of 3 like this 由于某种原因,我需要将每个值四舍五入为3
select round(4796.529,3)+ round(1585.0414,3)+ round(350.9863,3)+ round(223.3549,3)+ round(127.6314,3)+ round(479.6529,3)+ round(158.5041,3)
but when I take the sum they shows a very minor variation. 但是当我取总和时,它们显示出很小的变化。 first line of code returns 7721.7000
. 第一行代码返回7721.7000
。 and the second one 7721.6990
. 第二个是7721.6990
。 But this variation in not acceptable. 但是这种变化是不可接受的。 What is the best way to solve this ? 解决此问题的最佳方法是什么?
As Whencesoever said, your problem is mathmatical one, not a programming error. 正如Whencesoever所说,您的问题是数学问题,而不是编程错误。
I'd talk with the business and figure out where they want the rounding applied. 我会与企业讨论并弄清楚他们希望将舍入应用于何处。
Also, as a side note, MONEY
is a terrible datatype. 另外,作为一个注释, MONEY
是一种可怕的数据类型。 If you can, you may want to consider switching to a DECIMAL
. 如果可以,您可能需要考虑切换到DECIMAL
。 See Should you choose the MONEY or DECIMAL(x,y) datatypes in SQL Server? 请参阅是否应该在SQL Server中选择MONEY或DECIMAL(x,y)数据类型?
When you round numbers before you sum them you will get a different result than if you round numbers after you have summed them. 在对数字求和之前对数字进行四舍五入时,将获得与对数字求和后进行四舍五入的结果不同的结果。 Simple as that. 就那么简单。 There is no way to solve this. 没有办法解决这个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.