????? ???????????? Rounding Implement ???? ????

???? ?????? ???? ??????
???? ID: 196652 - ?? ???????? ?? ?????? ??? ?? ?? ???? ???? ???? ??.
??? ?? ??????? ???? | ??? ?? ??????? ????

?? ????? ??

??????

??? ????? rounding ?????????? ?? ??? ?????? Microsoft ???????? ??? ?????? ??? ??????????? ?? ??? ?????????? ?????? ??????? Rounding ?? ??? Excel ?? ?????????? Round() ????? ???? ?? ??? Visual Basic ??? CInt(), CLng() ?? Round() ?????? ??? Banker ?? Rounding rounding ????? ?? ???? rounding ?????? ???? ?? ?????? ?? ????? ???? ?? ??? ????? ?????? ???? ?? ?? ????????? ?? ??? ???-?? ??????? Visual Basic ?? ????? ???? ??? ???? ?????, ???? ??????? rounding ?????????? ?? ??????????? ????? ?????? ????? ???? ???

???? ???????

Explained rounding

?? ?? ???? ??????? ?? ??? ?????? ?? lesser ??????? ?? ?????? ??? ?????? ???? ????? ???, ?? ??? ???? ?? ??? ?????? ??? ???? ??????? ?????? ?? ?? ??? ???????? ??? ??? ????????? ?????? ??? ?????? ???? ?? ???????? ???

???? rounding

Simplest rounding ?? ??? ??? truncation ??? ????? ?????? ??????? ?? ??? ??? ?? ??? ?? ????? ???? ???? ??? VBA fix() ?????? ?? truncation ?? ?? ?????? ??? ?????? ?? ???, fix(3.5) ??? 3 ??, ?? fix(-3.5) ??-3 ???

Int() ?????? ???????? ?????? ???????? ??? ?? ?? ???? ?? ???? Int() ?? fix() ??????? ???????? - truncating - ?? ??? ???? ????? ?? ??? ???? ??? ????? ??????? ???????? ?? ??? ????? ?????? ?? ???: int(-3.5)-4 ???? ???

Fix() ?????? ?? ????????? rounding ??????? ?? ?? ?? ??? ????????? ?? ??????? ???????? ?? magnitude (???????? ???) ???????? ?? ?? ?????? ??? Int() ?????? ?? asymmetric rounding ??????? ?? ????????? ?? ??????? ???????? ?? magnitude differently ???????? ???? ?? ?? ?????? ???

Excel ?? ???? ?????????? ??????: int(), Floor() ?? RoundDown(). Int() ??? ????? ?? ??? ???? ?? ???? int() ?? Visual Basic ??? ????????? ?? ???? Floor() ??????? ????? ?? truncates ??, ????? ??????? ???????? ?? ??? ????? ???? ????? RoundDown() ?????? VBA fix() ?????? ?? ??? ??? ???? ????? ?? ??? ???? ???

Microsoft SQL Server ?? Round() ?????? ?? VBA fix() ??????? ???? ????? ?? ???? ???? SQL ????? ?? ?? ?? Floor() ??????, ?? VBA int() ?????? ?? ??? ??? ???? ????? ?? ??? ???? ???

??? rounding

SQL ????? ?? Excel ????? ?? ?????? Ceiling(), ?? ????? ??? (???? ???????) ?? ???? ??? ????? ???? ???????? ??? ???? ???

??????????? ?? ??? Visual Basic ?? ??? ???? ????? ?? ?????? ???? ??? ???????, ??????? ???????? ?? ??? fix() ?? int() ????? ???? ?? ???? ??????? ?????? upward, ??? ???? ?? ????

Towards 0 fix() ???? ???????? (?? ????? sense ??? ???? ????? ???????? magnitude ?? ????? ???)? Fix(-3.5),-3.5 ???

0 ?? ??? int() ???? ???????? (?? ??? ???????? magnitude, ????? ???? ????? sense ???)? Int(-3.5),-4 ???

??????? Rounding

?? rounding ????? ???? ?? ???, ??? ???? ?????? necessarily ?? ???? ???? ??? ?????? ???? ??? ?????? ?? ???, ??? ?? ???????? 1 ?? ??? ???? 1.9 ??, ???? lot ?? ?? ???? ?? ??? ?? ???????? ??? ?? 2? ?? ?? ???????? ?? 1.6 2.4 2 ???????? ???? ????? ????? ?? ??? ???? ???

???????, ???? ???? 1.5, ?? 1 ?? 2 ?? ??? equidistant ??? ?????, ?????? ????-????? ?? ?????? ???????? ???

?? ?? ???? ??? ??????????? ????????? ???????, ??? ????-?? ????? ???????? rounding ???? ??-.5 ?? ???? ???? ?? ??? -1, ?? ??? asymmetric ???????, ????-.5 ?? ???? ???? ?? ??? 0 ????

????? ???????? ????????? ??????? rounding ???:
Excel Round() ?????????? ????????
SQL ????? Round() ?????? ????????? ??????? rounding ???? ?? ???? ????

????? ????? asymmetric ??????? rounding ???:
Java ??? ?? ????????? ?? Round() ?????

??????????? ?? ??? Visual Basic ??????? rounding ???? ?? ?? ???? ?? ????? ???? ???

Banker ?? Rounding

?? ?? ??????? ??? ?????? ?? ???, ????? ?? ???.5 rounding ??? ?? ???? ?? ?? ???? ???? ???????? ?? ??? grows ???? ?????? ?????? ?? ???? Banker ?? rounding ?? ??? ???? ?? ???? ???? ?? ?? ????? ???

Banker ?? rounding rounds.5 ???-??? ?? ?? ?? ???-??? ???? ??? ????? ?? ?????? ?? ?????? ???, ??? ???? ?? ??? ?? 1.5 ?? 2.5 ??? 2, ?? 3.5 ?? 4 ?? ??? ????? 4.5 ???? ?? ???? Banker ?? rounding ????????? ???

??????????? ?? ??? Visual Basic, ??? ????? ???????? ?????? ????????? banker ?? rounding: CByte(), CInt(), CLng(), CCur() ?? Round().

Banker ?? rounding ?? Excel ?????????? ?????? ???? ????

????? Rounding

?? banker ?? rounding ??? bias ?? ???? ???? ??.5 ??? ?? ???? ??? truly ????? ??????? ??? rounding ?????? ???? ?? ??????? ?? ??? ?? ???????? ??? ?? ???? ???? ?? ???, ??? ???? ????? deliberately ??, ?? ?? ???? ?? ???? ?? ?? ????? ??? ??, ?? ????? ?? ???? ?? ????? ??? ?? ?????? ???? ?? ??? ????? rounding ???????????? banker ?? rounding ?? ?? ??? ????? ????? rounding ???? ???? ?? ?? ????? ??? ??? ?????? ????

??? Microsoft ???????? ?? ??????????? ????? rounding ????????? ?? ???? ?? ????? ?????

Rounding ????????

???????? rounding ?? rounding ??.5 ??.5 ???? ?? ??? successive ??? ???

??? Microsoft ???????? ?? ??????????? ???????? rounding ????????? ???

Round() ?????? Inconsistently ???? ???? ??? ??

Round() ?????? ?? ???? ???? ??? ??????? Microsoft ???????? ?? ??? ??? ???? ??????? historical ?????? ?? ????

????? ?????? ??????????? ?? ??? ?????? relates:
   Product                             Implementation
   ----------------------------------------------------------------------
   Visual Basic for Applications 6.0   Banker's Rounding
   Excel Worksheet                     Symmetric Arithmetic Rounding
   SQL Server                          Either Symmetric Arithmetic Rounding
                                       or Symmetric Round Down (Fix)
                                       depending on arguments

   Java Math library                   Asymmetric Arithmetic Rounding
				

Visual Basic 6.0 ?? ????????? 6.0 ?? ??? Visual Basic ??? Round() ?????? banker ?? rounding ????????? ???? ??? ?? ?? ???????? ????? ???? ???? ?? ??? ??? ???? ?? ??? ????? ????? ?? ?????? ?? ????????? ???? ?? ?? ??:
   Debug.Print Round(2.45, 1) returns 2.4.
				

????? ????

????? ?????? ?????? ??? ????? ???? ?? rounding ??????? ??????? ?? ?????? ?? ???????? ?? ??? ?? ????? ???? ????
   Number/Int./Fix/Ceiling/Asym. Arith./Sym. Arith./Banker's/Random/Alt.
   ---------------------------------------------------------------------
   -2.6   -3   -2  -2      -3          -3           -3       -3     -3
   -2.5   -3   -2  -2      -2          -3           -2       -2     -3
   -2.4   -3   -2  -2      -2          -2           -2       -2     -2
   -1.6   -2   -1  -1      -2          -2           -2       -2     -2
   -1.5   -2   -1  -1      -1          -2           -2       -1     -1
   -1.4   -2   -1  -1      -1          -1           -1       -1     -1
   -0.6   -1    0   0      -1          -1           -1       -1     -1
   -0.5   -1    0   0       0          -1            0       -1     -1
   -0.4   -1    0   0       0           0            0        0      0
    0.4    0    0   1       0           0            0        0      0
    0.5    0    0   1       1           1            0        1      1
    0.6    0    0   1       1           1            1        1      1
    1.4    1    1   2       1           1            1        1      1
    1.5    1    1   2       2           2            2        1      1
    1.6    1    1   2       2           2            2        2      2
    2.4    2    2   3       2           2            2        2      2
    2.5    2    2   3       3           3            2        3      3
    2.6    2    2   3       3           3            3        3      3
				

??? ??? ???????:
   Number/Int./Fix/Ceiling/Asym. Arith./Sym. Arith./Banker's/Random/Alt.
   ---------------------------------------------------------------------
   0.0    -9   0   9       3            0           0        1      0
				

??? ??? ??????? ????????:
   Number/Int./Fix/Ceiling/Asym. Arith./Sym. Arith./Banker's/Random/Alt.
   ---------------------------------------------------------------------
   -13.5  -18  -9  -9      -12          -15         -13      -13    -14
				

??? ??? ??????? ??????:
   Number/Int./Fix/Ceiling/Asym. Arith./Sym. Arith./Banker's/Random/Alt.
   ---------------------------------------------------------------------
   13.5   9    9   18      15           15          13       14     14
				

?????? rounding ??????? ??????? ?? ??? ???? ?????? ??? ????? ??? ?? ?????? ????????? ?? ??????? ????????, fix(), ????????? ??????? rounding, banker ?? rounding ?? rounding ??????? ?? ??? ???????? ??? ?? ?? ???? ???? ??? ???? ????? rounding ?? ??? ?????? ??????

??? ??, ??? ???????? ??? ?? ?? ??? ??????? ?? ??????? ???, banker ?? rounding, rounding ??????? ?? ????? rounding ?????? ???????? ??? ?? ?? ???? ???

????? Rounding ??????????-??????? ??????

????? ??? ?? ????? ?????? ???? ??? ??? ?????? rounding ?????? ?? ??? ????? ??????? ?? ?????? ????? ???

?????? ?????? ???? ?? ???:
   AsymDown      Asymmetrically rounds numbers down - similar to Int().
                 Negative numbers get more negative.

   SymDown       Symmetrically rounds numbers down - similar to Fix().
                 Truncates all numbers toward 0.
                 Same as AsymDown for positive numbers.

   AsymUp        Asymmetrically rounds numbers fractions up.
                 Same as SymDown for negative numbers.
                 Similar to Ceiling.

   SymUp         Symmetrically rounds fractions up - that is, away from 0.
                 Same as AsymUp for positive numbers.
                 Same as AsymDown for negative numbers.

   AsymArith     Asymmetric arithmetic rounding - rounds .5 up always.
                 Similar to Java worksheet Round function.

   SymArith      Symmetric arithmetic rounding - rounds .5 away from 0.
                 Same as AsymArith for positive numbers.
                 Similar to Excel Worksheet Round function.

   BRound        Banker's rounding.
                 Rounds .5 up or down to achieve an even number.
                 Symmetrical by definition.

   RandRound     Random rounding.
                 Rounds .5 up or down in a random fashion.

   AltRound      Alternating rounding.
                 Alternates between rounding .5 up or down.

   ATruncDigits  Same as AsyncTrunc but takes different arguments.
				

??? ?????? ?? ?? ???? ??: ?????? ???????? ???? ?? ?? ???????? ???????? ??? ??????? ???? ???? ??, ?? ?????? ???? ???????? ??? ???? ?? ??????? ??? ?? ?? ?? ?????? ???? ??? ??????? ????????? ??, ??? ?????? ?? ????? rounding ?????? ????? ?? ??? ??????? scaled ??? ?????? ?? ??? (2.55, 10) AsymArith 2.6 ???? ??, ???? ??????, ??? ???????? 1/??????? ???? ?? ??? 1/10 = = 0.1?

????? ???: 0 ?? ??????? ??????? ??? ??-???? ??????: 1/??????? = 1/0?

????? ?????? ?????? ?? ??????? ????? ?? ?????? ??:
   Expression       Result  Comment
   --------------------------------------------------------------------
   AsymArith(2.5)     3     Rounds up to next integer.
   BRound(2.18, 20)   2.2   Rounds to the nearest 5 cents (1/20 dollar).
   SymDown(25, .1)   20     Rounds down to an even multiple of 10.
				

??? ????? ?? ??? ????? ?? ADownDigits, ?? ?? ??????? ?? ???? ????? ????? ?? ?????? ?? ????????? ???? ?? ??? ???? ?????? ???? ?? ?? ???????? ?????? ?? ???
   Expression            Result Comment
   ---------------------------------------------------------------------
   ADownDigits(2.18, 1)    2.1  Rounds down to next multiple of 10 ^ -1.
				

????? ????


   Function AsymDown(ByVal X As Double, _
            Optional ByVal Factor As Double = 1) As Double
     AsymDown = Int(X * Factor) / Factor
   End Function

   Function SymDown(ByVal X As Double, _
            Optional ByVal Factor As Double = 1) As Double
     SymDown = Fix(X * Factor) / Factor
   '  Alternately:
   '  SymDown = AsymDown(Abs(X), Factor) * Sgn(X)
   End Function

   Function AsymUp(ByVal X As Double, _
            Optional ByVal Factor As Double = 1) As Double
   Dim Temp As Double
     Temp = Int(X * Factor)
     AsymUp = (Temp + IIf(X = Temp, 0, 1)) / Factor
   End Function

   Function SymUp(ByVal X As Double, _
            Optional ByVal Factor As Double = 1) As Double
   Dim Temp As Double
     Temp = Fix(X * Factor)
     SymUp = (Temp + IIf(X = Temp, 0, Sgn(X))) / Factor
   End Function

   Function AsymArith(ByVal X As Double, _
            Optional ByVal Factor As Double = 1) As Double
     AsymArith = Int(X * Factor + 0.5) / Factor
   End Function

   Function SymArith(ByVal X As Double, _
            Optional ByVal Factor As Double = 1) As Double
     SymArith = Fix(X * Factor + 0.5 * Sgn(X)) / Factor
   '  Alternately:
   '  SymArith = Abs(AsymArith(X, Factor)) * Sgn(X)
   End Function

   Function BRound(ByVal X As Double, _
            Optional ByVal Factor As Double = 1) As Double
   '  For smaller numbers:
   '  BRound = CLng(X * Factor) / Factor
   Dim Temp As Double, FixTemp As Double
     Temp = X * Factor
     FixTemp = Fix(Temp + 0.5 * Sgn(X))
     ' Handle rounding of .5 in a special manner
     If Temp - Int(Temp) = 0.5 Then
       If FixTemp / 2 <> Int(FixTemp / 2) Then ' Is Temp odd
         ' Reduce Magnitude by 1 to make even
         FixTemp = FixTemp - Sgn(X)
       End If
     End If
     BRound = FixTemp / Factor
   End Function

   Function RandRound(ByVal X As Double, _
            Optional ByVal Factor As Double = 1) As Double
   ' Should Execute Randomize statement somewhere prior to calling.
   Dim Temp As Double, FixTemp As Double
     Temp = X * Factor
     FixTemp = Fix(Temp + 0.5 * Sgn(X))
     ' Handle rounding of .5 in a special manner.
     If Temp - Int(Temp) = 0.5 Then
       ' Reduce Magnitude by 1 in half the cases.
       FixTemp = FixTemp - Int(Rnd * 2) * Sgn(X)
     End If
     RandRound = FixTemp / Factor
   End Function

   Function AltRound(ByVal X As Double, _
            Optional ByVal Factor As Double = 1) As Double
   Static fReduce As Boolean
   Dim Temp As Double, FixTemp As Double
     Temp = X * Factor
     FixTemp = Fix(Temp + 0.5 * Sgn(X))
     ' Handle rounding of .5 in a special manner.
     If Temp - Int(Temp) = 0.5 Then
       ' Alternate between rounding .5 down (negative) and up (positive).
       If (fReduce And Sgn(X) = 1) Or (Not fReduce And Sgn(X) = -1) Then
       ' Or, replace the previous If statement with the following to
       ' alternate between rounding .5 to reduce magnitude and increase
       ' magnitude.
       ' If fReduce Then
         FixTemp = FixTemp - Sgn(X)
       End If
       fReduce = Not fReduce
     End If
     AltRound = FixTemp / Factor
   End Function

   Function ADownDigits(ByVal X As Double, _
            Optional ByVal Digits As Integer = 0) As Double
     ADownDigits = AsymDown(X, 10 ^ Digits)
   End Function
				

????? ???: Excel ?? MRound() ?????????? ??????, ?????????? ?? exception ?? ??? - rounding ??? ?????? ?? ????? ?? ADownDigits, ???? ????? ???? ?? ???? ???? ??????? ????? ?? ?????? ????? ?? ???? ???

???? ???????? ??? ?? rounding ??????? ????? MRound(), ?? ???, ?? ??????? ?? ???? ????? ?? ??????? ???? ??? ???? ???? ?? ??? ???? ???? 10 ?? ??? ???? ?? ???? ADownDigits ?? ????? ?? ???? ?????? ??? ???? ???

????? ?????? ????????

??? ???? ???????? ??? ?? rounding ??????? ?? ????? ???? ??????, ???? 15 ????? ??? ?? ???????????? ?? ???? ??? ?? ?? ????? ?????

??????? ??? ?? ??? ?????????? ??? expressed ?? ?? ???? ???, ??????? ?? ?? ???? ?? ??????? ????????? ?????? ??????? ???????? ??? ???????? ??? ?? ??? ???? ?????

?????? ?? ???, ?????? 2.25 ?? ???? ?? ???????? internally 2.2499999..., ???? ????? ??????? rounding ?? ???, ?? ?? ??? ??? ?? ???? ?? ???? ???? ??????? ?? ???? ??? ??? ??, ???? ?????? ???? ?? ?????? ??, ???? ??????? ?? ?? ?? ideal ????? ??? ???????? ?????? ??? deviate ????? ??? ?????? ???

??? ?? ?????? ??, ?? ?? ???? ?? ?? ???? ????? ???? ??????, ??????, ?? 4 ????? ??????? ?? ???? ?? ???? ' ??????

?? ???? ?????? Variant ?? ????? ???? ?? ??? ????? ???? ??????, ?? ???? ?? ?? ??? ?????? ???? ?? ??? CDec() ??????? ???? ?? ??? 28 ????? ??? ???? ?? ????? ?? ?? ???? ???

?????? ????? rounding

?? ?? ?????? ???? ?????? ?? ?????, 4 ????? ??? ???? ?? ??? ??? ??, ?? ?? ?????????? ???????? ????? ????? ????? ??? 2 ?? ??? cents ?? ????

???? Round2CB ?????? ?? ??? ?????-coded variation ?? rounding ????? ??? 2 banker ?? ???? ??, ????? ??? ?????? ???? ???? ??? ?? avoids ???? ???????? ???? ??? ??????? ?????? approaching ?????? ???? ?????? ?? ???? ???
   Function Round2CB (ByVal X As Currency) As Currency
     Round2CB = CCur(X / 100) * 100
   End Function
				

????? ??? rounding

Asymmetric ??????? rounding ????? ???? ?????? ?? ????? ???? ?? ?? ?????? ?????????? ??:
   Function AsymArithDec(ByVal X As Variant, _
            Optional ByVal Factor As Variant = 1) As Variant
     If Not IsNumeric(X) Then
       AsymArithDec = X
     Else
       If Not IsNumeric(Factor) Then Factor = 1
       AsymArithDec = Int(CDec(X * Factor) + .5)
     End If
   End Function
				

Rounding ??? ???? ??????? ?? ??? ??? ??????? ?? ??????

???????? ??? taught ?? ??? ??? rounding ?? ????? ?? ??????? rounding ?? ??????? ?????? ?? ????? ?? ??? ??? Rounding ?? ?? ?????? ?? ??? ???? ???? ?????? 1 ??? ????? ???? ?? ??? rounding ???? ?? ??? ??? ???? ?? ???? ?? ???? ????? ????? ????? ??? ?? ????? ???? ??? ????? ???? ????? ?????? ???, ??????? ????? ??? rounding ???? ?? ??? ??????? ?? ??? ????

?????? ?? ???, 2.5 ?? 2.51 ??? ?? 3, ?? 2.4 ?? 2.49 ????? ???? ?? ??? 2?

?? ?? banker ?? rounding (?? ???? ???? ?? ?? ?? ?? ??? ?? ????.5 ?? ???) ?? ????? ?? ?? ?? ???????? ??????? ???????? asymmetric ??????? rounding ?? ????? ???? ???, ??????? ?? ??? ?? ???? ??? ??????? ???? ?? ??? ??? ?????? ???? ?? ?? ???? ?? ? ??? ?????? ?????? ?? ????

?????? ?? ???, banker ?? rounding, ?? ??? 2.5 ???????? 2 ?? 2.51 rounds ???? ?? ??? ?? 3?

???? asymmetric ??????? rounding, ?? ???-2.5 ???????? ??-2 ???-2.51 ????????-3 ???? ?? ????

????-??????? ?????? ?? ???? ??? ???????? ?? ?? ?????? ????? ??????? ???? ??? ?? ?? rounding ?? ??? ???

??????

Visual Basic ??????, ??????? 6.0; ????: Int, ??????? ???????; ??????? ??????

Microsoft Transact SQL ???; ????: ??????? ?????? ?? ??????; ?????? ??????

(?) Microsoft Corporation 1998, ??? ?????? ????????? ???????? Malcolm ?????????, Microsoft Corporation ?????? ???

???

???? ID: 196652 - ????? ???????: 02 ?????? 2010 - ??????: 3.0
???? ???? ???? ??:
  • Microsoft Visual Basic 5.0 Control Creation Edition
  • Microsoft Visual Basic 5.0 Learning Edition
  • Microsoft Visual Basic 5.0 Professional Edition
  • Microsoft Visual Basic 6.0 Professional Edition
  • Microsoft Visual Basic 5.0 Enterprise Edition
  • Microsoft Visual Basic for Applications 6.0
  • Microsoft SQL Server 6.0 Standard Edition
  • Microsoft SQL Server 6.5 Standard Edition
  • Microsoft SQL Server 7.0 Standard Edition
??????: 
kbhowto kbmt KB196652 KbMthi
???? ?????? ????????
??????????: ?? ???? ?? ???? ??????? ?? ????? ?? Microsoft ????-?????? ?????????? ?????? ?????? ???? ??? ??. Microsoft ???? ??? ????-???????? ?? ????-???????? ????? ?????? ?? ???? ???????? ???? ?? ???? ????? ????? ??? ?? ??? ?????? ?? ???? ???? ???? ??? ????? ??. ???????, ????-???????? ???? ????? ???? ???? ???? ???. ?????, ????????, ?????-???? ?? ??????? ?? ???????? ?? ???? ???, ???? ?? ??? ?????? ???? ???? ??? ????? ??? ?? ???? ??. Microsoft ??????? ??? ???? ?? ?????? ?? ??????????, ????????? ?? ??? ?????? ?? ???? ????? ?? ???? ???????? ?? ??? ???? ????? ?? ??? ????????? ???? ??. Microsoft ????-?????? ?????????? ?? ????? ?????? ?? ?? ??? ??.
?????????? ?? ??????? ????????? ??????? ??:196652

??????????? ???

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com