2012-07-24 42 views
0

下面的代碼工作正常,但它並沒有將存儲的值舍入到最近的便士,例如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 

乾杯 羅斯

回答

1

「Round函數執行舍入到偶數,這是從圓形到較大的不同。」 --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

+0

感謝隊友最有幫助 – Ross 2012-07-24 12:46:58

+0

當我把「#.00」我得到一個代碼錯誤輪((費用/0.8)+(VAL/0.8)+ OutLays,「#.00」)是這個應該是它的地方? – Ross 2012-07-24 14:15:05

+0

#.00帶格式,不輪。格式((費用/ 0.8)+(VAT/0.8)+ OutLays,「#.00」)' – Fionnuala 2012-07-24 14:16:03

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空值,這可能不是理想的所有情況。我經常在一些相當大的查詢中使用它,希望它可以幫助別人。

+0

有很多不同的舍入規則集。我專門尋找一種從零偏向於更大的數字。我知道「真正的」是一個用詞不當,因爲這不是最常用的四捨五入方法。如果沒有別的辦法,這可能會幫助某人調整自己的舍入函數以適應他們需要的任何規則。 – 2012-10-12 21:08:18