vmg
vmg

Reputation: 10566

VBScript and Float (double) numbers: Round function returns wrong result

such code in VBScript will return wrong result:

MsgBox Round(4.99985,4)

It will return 4.9998 but correct result is 4.9999. I know that it is related to how VBScript works with numbers with floating points, that some numbers can't be represented in binary system, but please tell me:

  1. What exactly happening here?
  2. What is the possible workaround for that?

Thanks!

Upvotes: 4

Views: 11217

Answers (1)

Helen
Helen

Reputation: 97540

This is expected result, so called bankers' rounding. Check out the description of the Round function (bold added by me):

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


To round to larger, 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.)

Upvotes: 5

Related Questions