1

NOTE; I know about Math.Floor and Math.Round and stuff. That’s not what I’m looking for.

I would like to know how VB.NET 2008 internally handles the rounding off of integers.

For example:

    Test1 = (127 / 2) + 13
    Test2 = (127 / 2) + 12

Both test variables will give back 76, which seems strange to me.

(127 / 2) = 63.5

So

63.5 + 13 = 76.5

63.5 + 12 = 75.5

Since both have a .5 it seems logical to me that they either get rounded off to above or below. So expected answers are either:

Floor: 76,75 or Ceiling: 77,76

But instead it seems that 76.5 gets floored while 75.5 is upped.

Any clue on what rules VB.NET uses for this?

Santosh Panda
  • 7,235
  • 8
  • 43
  • 56
Hugo Smits
  • 19
  • 1
  • 2
    it is called bankers rounding; `x.5` values are rounded to the nearest even number to prevent a bias. you can specify the mode using the Math.Round overload: http://msdn.microsoft.com/en-us/library/system.midpointrounding(v=vs.110).aspx – Ňɏssa Pøngjǣrdenlarp Oct 25 '14 at 14:52
  • In vb.net, when I know I'll have a bunch of midpoints divisions like that one above (finding margin/padding on some layout control) I convert each part of the division by 2 to Integer before adding something else, like `CInt(127 / 2) + X`, not `CInt((127 / 2) + X)`. But that doesn't solve the whole thing, just fix it on some very specific cases though. – Karl Stephen Oct 25 '14 at 16:52

0 Answers0