Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Rounding / Ceil function in VB6?

4,655 views
Skip to first unread message

Bent S. Lund

unread,
Jun 2, 2003, 5:43:17 AM6/2/03
to
Hello,

is there a function i VB6 that equals te Ceil function?

X = Ceil(1.2) returns the value 2

Or any other tricks that Returns the smallest integer greater than or equal
to its numeric argument.

Regards
Bent


Ben Taylor

unread,
Jun 2, 2003, 6:55:44 AM6/2/03
to
Try
int(x) - clng((x>=0 and x<>int(x)))

>.
>

NickD

unread,
Jun 2, 2003, 7:38:50 AM6/2/03
to
"Bent S. Lund" <bs...@online.no> wrote in message
news:OgERBuOK...@TK2MSFTNGP10.phx.gbl...

VB has 3 built in methods of rounding that I'm aware of.

1. Round() / CLng() / etc.

These types do "Banker's rounding" (I have another term for it..) which errs
towards the nearest even number from a .5 boundary. E.g. Round(11.5) =
Round(12.5). Personally I find this style of rounding to be shoddy and has
caused more to have to fix more than a few bugs in developer's code. What's
really funny is that I think it was either VB4 or VB5 they added the Round()
function and I thought "ah at last, a function that will round properly"
only to find it did exactly the same thing as the "Convert-to-<type>"
typecast functions -- what was the point of that?


2. Int()

Asymmetric rounding down. E.g. Int(3.5) = 3; Int(-3.5) = -4; Int(2.9) = 2.


3. Fix()

Symmetric rounding down. E.g. Fix(3.5) = 3; Fix(-3.5) = -3; Fix(2.9) = 2.


To implement what you're suggesting I believe you'll need to create your own
function. Something like:

Function Ceil(ByVal num As Double) As Long
Dim X As Long

X = Int(num)
Ceil = X + Iif(X = num, 0#, 1#)
End Function

Note that this will produce Ceil(3.1) = 4 and Ceil(-3.1) = -3. If you were
expecting -4 for the second result then you'd need to do modify it a little
further.. like this..

Function Ceil(ByVal num As Double) As Long
Dim X As Long, Y As Long

Y = Not (Abs(num) = num)
X = Int(num)
Ceil = X + IIf(X = num, 0#, 1# + Y)
End Function

If this looks like a lot of work for just some rounding, wait for Rick's
one-liner reply ;) Generally I don't use IIf() but it seems harmless enough
here and was used in an MSDN article I used for reference.

Regards,
Nick


Bent S. Lund

unread,
Jun 2, 2003, 8:28:09 AM6/2/03
to
Hi,

and thanks!

I rolled my own quick'n'dirty function to solve this:


Public Function Ceiling(Value As Variant)

'** Returns the smallest integer greater than or equal to its numeric
argument.
'** Ceiling(1.2) returns 2
'** Ceiling(0.2) returns 1

Dim tmpInput As Double

If IsNumeric(Value) Then
tmpValue = CInt(Value)

If tmpValue < Value Then
tmpValue = tmpValue + 1
ElseIf Value = 0 Then
Ceiling = 0

Else
tmpValue = tmpValue

End If
Else
tmpValue = 0

End If

Ceiling = tmpValue


End Function
"NickD" <no_emails...@nickd.demon.co.uk> wrote in message
news:%23iICFuP...@TK2MSFTNGP12.phx.gbl...

somesh...@gmail.com

unread,
Dec 5, 2018, 6:17:59 AM12/5/18
to
Thanks


0 new messages