VBScript and Float (double): Round function returns incorrect result

code like this in VBScript will return the wrong result:

MsgBox Round(4.99985,4)


It will return 4.9998

, but the correct result is 4.9999

. I know it has to do with the way VBScript works with floating point numbers, that some numbers cannot be represented in binary, but please tell me:

  • What exactly is going on here?
  • What's a possible workaround for this?



source to share

1 answer

This is the expected result, the so-called rounding of bankers . Check out the functionRound

description (in bold):

The Round function works round to even, which differs from round to larger .... If the expression is halfway between two possible rounded values, the function returns the possible rounded value whose rightmost digit is an even number .

To round up to more, you can use the following function (taken from here ):

Function RoundToLarger(ByVal Number, ByVal NumDigitsAfterDecimal)
  RoundToLarger = CDbl(FormatNumber(Number, NumDigitsAfterDecimal))
End Function


(Note: negative numbers are rounded down.)



All Articles