Lottery Post Journal

VB Shortcut for ceiling() function

For some reason, Visual Basic does not include a ceiling() function, which takes a fractional number, and gives you the next higher integer.  For example:

  • ceiling(1) = 1
  • ceiling(1.7) = 2
  • ceiling(1.1314) = 2
  • ceiling(0.1) = 1
  • ceiling(-1.1) = -1

And so on.

Well, I needed a ceiling() function this evening, so I figured out a very cool trick, without resorting to creating a new function.  To get the ceiling of a number x, do this:

(-(int(-(x))))

The int() of a negative number returns the next lower number, not what you'd think, but perfect for our ceiling function.  Then, just get the negative of that result.  (I use all the parenthesis to make sure VB understands exactly what I'm trying to do.)

1 Comments:

  • great work. i'll have to file this one away in back of my brain somewhere. i know i'll need it some day.

    By JADELottery, at 10:00 PM

Post a Comment

<< Home