访问中的舍入

时间:2012-07-24 12:29:51

标签: ms-access

以下代码工作正常,但它没有将存储的值四舍五入到最近的便士,例如8.025即将到来而不是8.01,任何人都可以提出修复吗?

Public Function Fs_Update_AccInvoices_Nexum() As Boolean
    Dim adoRsInvoiceDCID As New ADODB.Recordset
    Dim adoRsNexumInvoices As New ADODB.Recordset

    On Error Resume Next
    adoRsInvoiceDCID.Open "SELECT * FROM [tInvoiceDCID] where Issued=0" _
        , CurrentProject.Connection, 2, 2
    While Not adoRsInvoiceDCID.EOF
        adoRsNexumInvoices.Open "SELECT * FROM [tPrintInvoiceNumbersNexum] " _
            & " WHERE InvoiceID=" & adoRsInvoiceDCID("InvoiceID") _
            , CurrentProject.Connection, 2, 2
        If Not adoRsNexumInvoices.EOF Then
            DoCmd.SetWarnings off
            DoCmd.RunSQL "Update [Acc Invoices t Nexum] " _
                & " SET [Total Due] = Round((Fees/0.8)+(VAT/0.8)+OutLays,2)" _
                & " Fees = Round(Fees/0.8,2), VAT = Round(Vat/0.8,2)" _
                & " WHERE Invoice=" & adoRsNexumInvoices("PrintingasINVOICE")
        End If
        adoRsNexumInvoices.Close

        adoRsInvoiceDCID.MoveNext
    Wend
    adoRsInvoiceDCID.Close
End Function

干杯 罗斯

2 个答案:

答案 0 :(得分:2)

快速说明: 我注意到vba的舍入函数有些不准确,格式函数无法修复。在我的特殊情况下,我试图围绕数字3687.23486

轮次(3687.23486)= 3687.23

格式(3687.23486,“#。00”)= 3687.23

在传统的最接近规则下,这应该导致3687.24 我已经看到几个自定义函数发布到各种论坛来解决舍入问题,但没有一个对我有用,所以我自己写了。

    Function trueRound(ByVal varNumber As Variant, ByVal intDecimals As Integer) As Double
    If IsNull(varNumber) Then
        trueRound = 0
        Exit Function
    End If
    Dim decimals As Integer, testNumber As Double
    decimals = 0
    If InStr(varNumber, ".") > 0 Then decimals = Int(Len(varNumber)) - Int(Len(Fix(varNumber)) + 1)
    If decimals = 0 Or intDecimals > decimals Then
        trueRound = varNumber
        Exit Function
    End If
    Do Until Len(varNumber) - Len(Fix(varNumber)) - 1 <= intDecimals
        testNumber = varNumber * 10 ^ (decimals - 1)
        varNumber = Round(testNumber, 0) / 10 ^ (decimals - 1)
        decimals = decimals - 1
    Loop
    trueRound = varNumber
End Function

我把它快速地删除了,因此没有错误处理,并且传递给函数的空值导致0,这可能不适合所有情况。我经常在一些非常大的查询中使用它,希望它可以帮助其他人。

答案 1 :(得分:1)

&#34; Round函数执行round到even,这与round到large不同。&#34; --Microsoft

Debug.Print Round(19.955, 2)
'Answer: 19.95

Debug.Print Format(19.955, "#.00")
'Answer: 19.96

另见How to Round in MS Access, VBA