[英]Incorrect sum of decimal values in vb.net
I'm pulling data from a T-SQL procedure, one of my columns is cast as numeric(18,2) 我从T-SQL过程中提取数据,我的一个列被转换为数字(18,2)
When i run this procedure and copy the data to excel, the sum on this column is 0.01 less than the sum i get when i run this on vb.net. 当我运行此过程并将数据复制到excel时,此列上的总和比我在vb.net上运行时获得的总和少0.01。
In vb.net, i am aggregating the values for the column into a dictionary(of string, decimal) 在vb.net中,我将列的值聚合为字典(字符串,十进制)
I don't know why this happening. 我不知道为什么会这样。
If Not dr.IsDBNull(dr.GetOrdinal(f.field)) Then
Dim dval = If(dr.IsDBNull(dr.GetOrdinal(f.field)), 0D, dr.GetDecimal(dr.GetOrdinal(f.field)))
di.Add(New FieldValue With {.Type = f, .Value = If(rpt.allow_negatives, dval, Math.Abs(dval))}, rpt.overide_format)
If rpt.has_total AndAlso f.totaled Then totals(f.field) += dval
Else
di.Add(New FieldValue With {.Type = f, .Value = 0D})
End If
Math.Abs has a decimal
overload, so it is probably not that. Math.Abs有一个decimal
重载,所以可能不是这样。 More likely it is Excel that is getting the calculation wrong. 更有可能是Excel导致计算错误。 Excel typically throws away anything more than 15 digits of precision. Excel通常会丢弃超过15位数的精度。
See : http://en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel 请参阅: http : //en.wikipedia.org/wiki/Numeric_precision_in_Microsoft_Excel
To prove this to yourself, type 要向自己证明这一点,请键入
1234567890123456.78
into an Excel cell. 进入Excel单元格。 You'll see that the number ends up being : 你会看到这个数字最终是:
1234567890123450
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.