13

VBA Accessで丸める最良の方法は何ですか?

私の現在の方法はExcelの方法を利用しています

Excel.WorksheetFunction.Round(...

しかし、私はExcelに依存しない手段を探しています。

4

13 に答える 13

26

VBA の Round 関数は Banker の丸めを使用し、次のように .5 を偶数に丸めることに注意してください。

Round (12.55, 1) would return 12.6 (rounds up) 
Round (12.65, 1) would return 12.6 (rounds down) 
Round (12.75, 1) would return 12.8 (rounds up)   

一方、Excel ワークシート関数 Round は、常に 0.5 を切り上げます。

いくつかのテストを行ったところ、セルの書式設定や列幅の丸め (General Number 形式を使用する場合) にも .5 の丸め (対称的な丸め) が使用されているようです。「表示される精度」フラグは、丸め自体を行うようには見えず、セル形式の丸められた結果を使用するだけです。

丸めのために Microsoft の SymArith 関数を VBA に実装しようとしましたが、58.55 のような数値を指定しようとすると、Fix でエラーが発生することがわかりました。58.6 ではなく 58.5 の結果を返す関数。その後、次のように、Excel Worksheet Round 関数を使用できることを最終的に発見しました。

アプリケーション.ラウンド(58.55, 1)

これにより、VBA で通常の丸めを行うことができますが、一部のカスタム関数ほど速くはない場合があります。これは質問から完全に一周したことを認識していますが、完全を期すために含めたいと思いました。

于 2008-09-26T00:53:59.860 に答える
10

受け入れられた答えを少し拡張するには:

「ラウンド関数は、ラウンドから偶数へのラウンドを実行します。これは、ラウンドからラージへのラウンドとは異なります。」
--Microsoft

フォーマットは常に切り上げられます。

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

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

ACC2000:浮動小数点数を使用する場合の丸め誤差:http ://support.microsoft.com/kb/210423

ACC2000:必要な増分で数値を切り上げるまたは切り下げる方法:http ://support.microsoft.com/kb/209996

ラウンド関数: http: //msdn2.microsoft.com/en-us/library/se6f2zfx.aspx

カスタム丸め手順を実装する方法:http ://support.microsoft.com/kb/196652

于 2008-11-05T21:21:20.980 に答える
4

スイスでは、特に保険業界では、現金化するかどうか、利益などに応じて、いくつかの丸めルールを使用する必要があります。

現在使用している機能

Function roundit(value As Double, precision As Double) As Double
    roundit = Int(value / precision + 0.5) * precision
End Function

これはうまくいくようです

于 2008-10-03T10:46:45.740 に答える
2

IntとFixはどちらも便利な丸め関数であり、数値の整数部分を提供します。

Intは常に切り捨てられます-Int(3.5)= 3、Int(-3.5)= -4

修正は常にゼロに向かって丸められます-修正(3.5)= 3、修正(-3.5)= -3

強制関数、特にCIntとCLngもあり、整数型またはlong型に数値を強制しようとします(整数は-32,768〜32,767、longは-2,147,483,648〜2,147,483,647)。これらは両方とも最も近い整数に向かって丸められ、.5からゼロから丸められます-CInt(3.5)= 4、Cint(3.49)= 3、CInt(-3.5)=-4など。

于 2008-09-26T00:37:49.773 に答える
2
1 place = INT(number x 10 + .5)/10
3 places = INT(number x 1000 + .5)/1000

VBA は別のメモリ空間で動作するように見えるため、このように明らかに下品なソリューションは、Excel 関数を使用するよりもはるかに高速であることがよくあります。

たとえばIf A > B Then MaxAB = A Else MaxAB = B、ExcelWorksheetFunction.Max を使用するよりも約 40 倍高速です。

于 2008-10-01T00:28:21.913 に答える
1

bugランスは、VBAの実装における継承の丸めについてすでに言及しました。そのため、VB6アプリに実際の丸め関数が必要です。これが私が使っているものです。コメントに示されているように、それは私がウェブ上で見つけたものに基づいています。

' -----------------------------------------------------------------------------
' RoundPenny
'
' Description:
'    rounds currency amount to nearest penny
'
' Arguments:
'    strCurrency        - string representation of currency value
'
' Dependencies:
'
' Notes:
' based on RoundNear found here:
' http://advisor.com/doc/08884
'
' History:
' 04/14/2005 - WSR : created
'
Function RoundPenny(ByVal strCurrency As String) As Currency

         Dim mnyDollars    As Variant
         Dim decCents      As Variant
         Dim decRight      As Variant
         Dim lngDecPos     As Long

1        On Error GoTo RoundPenny_Error

         ' find decimal point
2        lngDecPos = InStr(1, strCurrency, ".")

         ' if there is a decimal point
3        If lngDecPos > 0 Then

            ' take everything before decimal as dollars
4           mnyDollars = CCur(Mid(strCurrency, 1, lngDecPos - 1))

            ' get amount after decimal point and multiply by 100 so cents is before decimal point
5           decRight = CDec(CDec(Mid(strCurrency, lngDecPos)) / 0.01)

            ' get cents by getting integer portion
6           decCents = Int(decRight)

            ' get leftover
7           decRight = CDec(decRight - decCents)

            ' if leftover is equal to or above round threshold
8           If decRight >= 0.5 Then

9              RoundPenny = mnyDollars + ((decCents + 1) * 0.01)

            ' if leftover is less than round threshold
10          Else

11             RoundPenny = mnyDollars + (decCents * 0.01)

12          End If

         ' if there is no decimal point
13       Else

            ' return it
14          RoundPenny = CCur(strCurrency)

15       End If

16       Exit Function

RoundPenny_Error:

17       Select Case Err.Number

            Case 6

18             Err.Raise vbObjectError + 334, c_strComponent & ".RoundPenny", "Number '" & strCurrency & "' is too big to represent as a currency value."

19          Case Else

20             DisplayError c_strComponent, "RoundPenny"

21       End Select

End Function
' ----------------------------------------------------------------------------- 
于 2009-02-25T16:46:12.373 に答える
1

整数値への丸めについて話している場合 (および小数点以下n桁に丸めない場合) は、常に古い学校の方法があります。

return int(var + 0.5)

(これを小数点以下n桁でも機能させることができますが、少し面倒になり始めます)

于 2008-09-26T01:09:37.903 に答える
0

Access 2003 で常に次の整数に切り上げる簡単な方法を次に示します。

BillWt = IIf([Weight]-Int([Weight])=0,[Weight],Int([Weight])+1)

例えば:

  • [重量] = 5.33 ; Int([重量]) = 5 ; 5.33-5 = 0.33 (<>0) なので、答えは BillWt = 5+1 = 6 です。
  • [Weight] = 6.000, Int([Weight]) = 6 なので、6.000-6 = 0 なので、答えは BillWt = 6 です。
于 2009-12-30T16:35:06.973 に答える
0
VBA.Round(1.23342, 2) // will return 1.23
于 2008-09-26T00:47:39.190 に答える
0
Public Function RoundUpDown(value, decimals, updown)
If IsNumeric(value) Then
        rValue = Round(value, decimals)
        rDec = 10 ^ (-(decimals))
        rDif = rValue - value
        If updown = "down" Then 'rounding for "down" explicitly.
            If rDif > 0 Then ' if the difference is more than 0, it rounded up.
                RoundUpDown = rValue - rDec
            ElseIf rDif < 0 Then ' if the difference is less than 0, it rounded down.
                RoundUpDown = rValue
            Else
                RoundUpDown = rValue
            End If
        Else 'rounding for anything thats not "down"
            If rDif > 0 Then ' if the difference is more than 0, it rounded up.
                RoundUpDown = rValue
            ElseIf rDif < 0 Then ' if the difference is less than 0, it rounded down.
                RoundUpDown = rValue + rDec
            Else
                RoundUpDown = rValue
            End If
        End If

End If
'RoundUpDown(value, decimals, updown) 'where updown is "down" if down. else rounds up. put this in your program.
End Function
于 2021-11-08T22:23:08.237 に答える