Domanda

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 )
È stato utile?

Soluzione

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.)

Altri suggerimenti

You could do

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

Adding 0.5 being the addition to your code. I don't think there is a built in Ceil function in VBScript?

Rounding will always go down i believe. So adding 0.5 will result in the output you're looking for.

0.1 + 0.5 = 0.6 Round(0.6) = 0

0.7 + 0.5 = 1.2 Round(1.2) = 1

Round is a pain it vbs

here are some functions i found on the internet that have helped me through the years

Function Floor(Number)
   Floor = Int(Number)
End Function

Function Ceil(Number)
    Ceil = Int(Number)
    If Ceil <> Number Then Ceil = Ceil + 1
End Function

Function Rounding(Number)
   Rounding = Floor(Number)
   If Number - Rounding >= .5 Then Rounding = Rounding + 1
End Function

Round doesn't work quite like you are expecting. From MSDN:

The Round function performs round to even, which is different from round to larger. The return value is the number closest to the value of expression, with the appropriate number of decimal places. If expression is exactly halfway between two possible rounded values, the function returns the possible rounded value whose rightmost digit is an even number. (In a round to larger function, a number that is halfway between two possible rounded values is always rounded to the larger number.)

It seems the rounding algorithm isn't quite that clear-cut as even that implies in the examples, and this looks like another example of where the result is slightly different than expected.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top