0

Why ASP and VBS when rounding if the integer part is odd and the decimal part is 5 the results go down, even when the result goes up ?

Please try it yourself:

a=1.5 
wscript.echo clng(a)   '---> 2 

a=2.5 
wscript.echo clng(a)   '---> 2 

a=3.5 
wscript.echo clng(a)   '---> 4 

a=4.5 
wscript.echo clng(a)   '---> 4

a=5.5 
wscript.echo clng(a)   '---> 6

a=6.5 
wscript.echo clng(a)   '---> 6

How is it possible? I haven't found any documentation about this...

Sorry , the question is why for default VBS/ASP e VBA usr Banker's rounding?

  • 2
    *"I haven't found **any** documentation about this"* - I find that very hard to believe. – user692942 Aug 04 '16 at 09:23
  • You don't want it to round use [`Int() or Fix()`](https://msdn.microsoft.com/en-us/library/t4dseb50%28v=vs.84%29.aspx?f=255&MSPPError=-2147217396) respectively. – user692942 Aug 04 '16 at 09:27
  • Although not VBScript *(VBA but difference is minimal)* this answer gives a good explanation of the behaviour - [How to round a number in VBA to the nearest 5? (or 10 or X)](http://stackoverflow.com/a/12731182/692942) – user692942 Aug 04 '16 at 09:36
  • I think it is absurd that by default all function VBS / ASP and VBA use the banker's rounding. – user2765668 Aug 04 '16 at 12:50

1 Answers1

4

From The Microsoft Developer Network
When the fractional part of a value is exactly 0.5, the CLng function rounds to the closest even number. For example, 0.5 rounds to 0, 1.5 rounds to 2, and 3.5 rounds to 4. The purpose of rounding to the closest even number is to compensate for a bias that could accumulate when many numbers are added together.

CLng differs from the Fix and Int functions, which truncate, rather than round, the fractional part of a number.

user692942
  • 16,398
  • 7
  • 76
  • 175
MC ND
  • 69,615
  • 8
  • 84
  • 126