以文本方式查看主题

-  Foxtable(狐表)  (http://www.foxtable.com/bbs/index.asp)
--  专家坐堂  (http://www.foxtable.com/bbs/list.asp?boardid=2)
----  关于用代码计算数值精度问题  (http://www.foxtable.com/bbs/dispbbs.asp?boardid=2&id=144262)

--  作者:南山南
--  发布时间:2019/12/15 17:48:00
--  关于用代码计算数值精度问题
例如:5173.41 / 6
用计算机计算出的值:862.235
用代码计算出的值:862.16667(精度偏低,假如计算几百万,上千万,这误差不就太大了)
代码如下:
If e.DataCol.Name = "公司" Then
    If e.NewValue Is Nothing Then
     e.DataRow("费用")=Nothing
     Else
    If e.DataRow.Isnull("费用") Then
            Dim Filter As String
            Dim ct,sm As Integer
            Filter = "日期 = #" & e.DataRow("日期") & "# And 编码 = \'" & e.DataRow("编码") & "\'"
            sm = DataTables("广告费").Compute("sum([消费])",Filter)
            ct = DataTables("分摊").Compute("Count([编码])", Filter )
            e.DataRow("费用")=sm/ct
      End If
     End If
End If
请老师解惑

--  作者:y2287958
--  发布时间:2019/12/15 19:17:00
--  
试试高精度
--  作者:有点蓝
--  发布时间:2019/12/15 20:33:00
--  
Dim ct,sm As Decimal

另外把费用列改为高精度列