Search
« Filter files into silo folders | Main | Command Line Utility to Create Test Files »
Friday
Jul312009

A Ceiling Function for VB Script

Annoyingly VB Script does not have a "Ceiling" function, i.e. one that takes a number and rounds it 'up' to the nearest integer (whole number).  So since I had need of one I wrote one.

Function Ceil(p_Number)
    Ceil = 0 - INT( 0 - p_Number)
End Function

There's not much to it admittedly, but I thought I'd share it just in case :)

So in case it still doesn't make sense I figure the best way to illustrate what's going on is comparing it to the Round() function which rounds off a number to the nearest integer.

Option Explicit

CeilTest 1
CeilTest 1.8
CeilTest 1.2
CeilTest -1.8
CeilTest -1.2

Function CeilTest(p_Number)
    MsgBox "Round = " & Round(p_Number) & vbCrLf & "Ceil = " & Ceil(p_Number),,"Value = " & p_Number
End Function

Function Ceil(p_Number)
    Ceil = 0 - INT( 0 - p_Number)
End Function
This returns the following set of results.

#Value Round(#Value) Ceil(#Value)
1 1 1
1.8 2 2
1.2 1 2
-1.8 -2 -1
-1.2 -1 -1

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (1)

Hello. Thank you for this great info! Keep up the good job!

August 7, 2009 | Unregistered Commenterjohnny

PostPost a New Comment

Enter your information below to add a new comment.
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>