0

I used this code to print some round outputs.

Private Sub TestRound()

    Dim i As Double
    Dim vTest As Double

    vTest = 17.005

    For i = 1 To 10
        Debug.Print vTest, Round(vTest, 2)
        vTest = vTest + 0.01
    Next i

End Sub

Here are the outputs (initial value, rounded value):

( 17.005,        17 )
( 17.015,        17.02 )
( 17.025,        17.03 )
( 17.035,        17.04 )
( 17.045,        17.05 )
( 17.055,        17.06 )
( 17.065,        17.07 )
( 17.075,        17.08 )
( 17.085,        17.09 )
( 17.095,        17.1 )
braX
  • 11,506
  • 5
  • 20
  • 33
OWing
  • 11
  • 2
  • 2
    https://learn.microsoft.com/en-us/dotnet/visual-basic/language-reference/data-types/double-data-type "Precision. When you work with floating-point numbers, remember that they do not always have a precise representation in memory." – Cody G Dec 12 '18 at 13:42
  • 2
    In VBA, double is a IEEE 64-bit (8-byte) double-precision floating-point number, 17.005 is most closely represented as `1.70049999999999990052401699359E1` So, when you round to two decimals, you get 17.00. See http://www.binaryconvert.com/result_double.html?decimal=049055046048048053 to play around – Cody G Dec 12 '18 at 13:49
  • In your case you could use `Application.WorksheetFunction.Round(vTest, 2)` if you find the above explained `Round` behaviour annoying. – JvdV Dec 12 '18 at 13:50
  • Thank you Cody. I was going for banker's rounding JvdV. – OWing Dec 12 '18 at 13:51
  • 2
    The reason is vba rounding is banker's rounding. Which means on 5 it will round towards the even value. For mathematical rounding use application.round – Scott Craner Dec 12 '18 at 13:53

0 Answers0