VB Shortcut for ceiling() function

Published:

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

Entry #82

Comments

Avatar JADELottery -
#1
great work. i'll have to file this one away in back of my brain somewhere. i know i'll need it some day.

Post a Comment

Please Log In

To use this feature you must be logged into your Lottery Post account.

Not a member yet?

If you don't yet have a Lottery Post account, it's simple and free to create one! Just tap the Register button and after a quick process you'll be part of our lottery community.

Register