A Ceiling Function for VBScript
31 Jul 2009Annoyingly VBScript 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 :)
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 |