Search code examples
asp-classicrounding-error

Rounding Error in ASP (vbscript)


I know very little about ASP or Vb Script. I am trying to troubleshoot an error in which a procedure, written in classic ASP, is unexpectedly rounding to a smaller, ODD number.

As I understand it, Round() should 'round to even'. Since 720 * 51 / 160 = 229.5, Round(720 * 51 / 160, 0) should equal 230. However, the ASP page consistently returns 229.

The actual ASP code is copied below. The variables in this instance are as follows:

FreeElig = 51
RedcElig = 0
PaidElig = 109
TotMlsSrvAms = 720
MlsSrvAmsFr is returning the questionable value.

I appreciate any help you can provide.

Thank you.

Sub ClaimCalcs()

Dim tmpTtlEnroll, tmpFreeEnroll, tmpRedcEnroll, tmpPaidEnroll, tmpPct
Dim GreaterValue

tmpFreeEnroll = CLng(SetZero(FreeElig)) 
tmpRedcEnroll = CLng(SetZero(RedcElig)) 
tmpPaidEnroll = CLng(SetZero(PaidElig))

tmpTtlEnroll = tmpFreeEnroll + tmpRedcEnroll + tmpPaidEnroll

If tmpTtlEnroll > 0 Then 
    tmpPct = tmpFreeEnroll / tmpTtlEnroll
Else    
    tmpPct = 0
End If

MlsSrvAmsFr = Round(CLng(SetZero(TotMlsSrvAms)) * tmpPct, 0 )

Solution

  • MSDN has this to say about the Round() function in VBA:

    Although the Round function is useful for returning a number with a specified number of decimal places, you cannot always predict how it will round when the rounding digit is a 5. How VBA rounds a number depends on the internal binary representation of that number.

    It does not say the same thing about VBScript, but I venture to guess that the same thing is going on.

    Bottom line is, if you want a Round() function that will actually behave predictably, you'll have to write your own. :/

    (If you want a function that does "normal" rounding, not some fancy-shmancy "round to even" formula, use FormatNumber(). As far as I know, that one actually behaves predictably, i.e. x.5 will always round up.)