|
-
Aug 2nd, 2004, 12:24 PM
#1
Ceiling in VB [RESOLVED]
I'm just stuck with this, what's the VB function that yields the "ceiling" of a number, i.e. the smallest integer greater of equal than the number?
For instance:
2.1 => 3
3 => 3
3.7 => 4
So it's complementary to the int function that produces the largest integer less or equal than the number.
If it doesn't exist, what would be a way to emulate it?
Last edited by krtxmrtz; Aug 2nd, 2004 at 03:00 PM.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 2nd, 2004, 12:27 PM
#2
are you thinking of the Round() function ?
casey.
-
Aug 2nd, 2004, 12:30 PM
#3
Originally posted by vbasicgirl
are you thinking of the Round() function ?
casey.
I first thought about it but it's not the same thing, for example,
Round(3.7 , 0) gives 3 instead of 4.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 2nd, 2004, 12:38 PM
#4
PowerPoster
You could try something like
x = mod(number)
if x > 0 then
number = int(number + 1)
endif
-
Aug 2nd, 2004, 01:23 PM
#5
Need-a-life Member
Try:
VB Code:
Private Function Ceil(sgNumber As Single) As Long
Ceil = Int(sgNumber) - (sgNumber - Int(sgNumber) > 0)
End Function
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 2nd, 2004, 01:40 PM
#6
Fanatic Member
just add 0.5 to the number you want to round.
round(1.2 + 0.5, 0 ) => 1
round(1.7 + 0.5, 0 ) => 2
- Use the thread tools to Mark your Thread as Resolved when your question is answered.
- Please Rate my answers if they where helpful.
-
Aug 2nd, 2004, 02:07 PM
#7
unless the number is negative.
FIX(-99.8) returns -99, so if you made the number negative, you'd get the next lower number, right?
Last edited by dglienna; Aug 2nd, 2004 at 02:11 PM.
-
Aug 2nd, 2004, 02:23 PM
#8
Need-a-life Member
In that case, maybe this is what you need:
VB Code:
Private Function Ceil(sgNumber As Single) As Long
Dim iDir As Integer
iDir = IIf(sgNumber < 0, -1, 1)
Ceil = Int(sgNumber) - (sgNumber - Fix(sgNumber) > 0) * iDir
End Function
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 2nd, 2004, 02:58 PM
#9
Originally posted by robbedaya
just add 0.5 to the number you want to round.
round(1.2 + 0.5, 0 ) => 1
round(1.7 + 0.5, 0 ) => 2
Thank you, but the function f I need must be such that f(1.2) = 2
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 2nd, 2004, 02:59 PM
#10
Originally posted by Mc Brain
In that case, maybe this is what you need:
VB Code:
Private Function Ceil(sgNumber As Single) As Long
Dim iDir As Integer
iDir = IIf(sgNumber < 0, -1, 1)
Ceil = Int(sgNumber) - (sgNumber - Fix(sgNumber) > 0) * iDir
End Function
Gracias Emiliano, this function and the previous one you've posted are actually what I was after.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 2nd, 2004, 03:05 PM
#11
Need-a-life Member
Por nada. Anyway, just in case you did not understand something...
Originally posted by krtxmrtz
Thank you, but the function f I need must be such that f(1.2) = 2
robbedaya's code did work. If you do
VB Code:
Debug.Print Round(1.2 + 0.5, 0 )
you would get 2 as the answer. The only problem was the negative numbers.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 2nd, 2004, 03:11 PM
#12
didnt realise you wanted to always round up.
what about
casey.
-
Aug 3rd, 2004, 02:42 AM
#13
Originally posted by Mc Brain
Por nada. Anyway, just in case you did not understand something...
robbedaya's code did work. If you do
VB Code:
Debug.Print Round(1.2 + 0.5, 0 )
you would get 2 as the answer. The only problem was the negative numbers.
I tried Round to begin with but discarded it because it doesn't work in a few instances where the number has no decimal part. For example,
y = Round(4 + 0.5, 0)
produces 4, but
y = Round(5 + 0.5, 0)
gives 6.
Could it be due to the internal floating representation? In principle the binary representations of both 4.5 and 5.5 should have the same mantissa.
I'd be curious to know why this unexpected behaviour takes place.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 3rd, 2004, 02:46 AM
#14
Originally posted by vbasicgirl
didnt realise you wanted to always round up.
what about
casey.
This one seems to work nicely! I don't know if there may be some erratic behaviour like in the Round function as I've described in my previous post, but I've tested it with a good deal of examples and have found no flaw.
Thanks casey, the simpler the better.
Last edited by krtxmrtz; Aug 3rd, 2004 at 03:13 AM.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 3rd, 2004, 11:10 AM
#15
i think its because round uses bankers rounding.
eg:
Round(1.5) = 2
Round(2.5) = 2
Round(3.5) = 4
Round(4.5) = 4
casey.
-
Aug 3rd, 2004, 11:19 AM
#16
Need-a-life Member
Originally posted by krtxmrtz
This one seems to work nicely! I don't know if there may be some erratic behaviour like in the Round function as I've described in my previous post, but I've tested it with a good deal of examples and have found no flaw.
Thanks casey, the simpler the better.
Beware with negative numbers. If yournumber is negative you'll get the smaller and not the bigger.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 3rd, 2004, 11:25 AM
#17
getting the smaller would be correct wouldnt it because it is actually being greater because of the negative ??
casey.
-
Aug 3rd, 2004, 11:31 AM
#18
Need-a-life Member
Originally posted by vbasicgirl
getting the smaller would be correct wouldnt it because it is actually being greater because of the negative ??
casey.
It is... but I understood he needed that way.
Emiliano F. Martín
If a post has helped you then please Rate it! (and give the user points he/she deserves by clicking on the image).
Encourage the person who helped you to keep doing it, and give him the points he deserves.
MP3 Organizer: Freeware to logically organize all your MP3s.
-
Aug 4th, 2004, 02:10 AM
#19
Originally posted by Mc Brain
It is... but I understood he needed that way.
As a matter of fact the figures I'm working with are the results of counting the number of elements in certain groups, so they're always positive. But it's nice to have McBrain's recipe for negative numbers too.
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
-
Aug 4th, 2004, 02:15 AM
#20
Originally posted by vbasicgirl
i think its because round uses bankers rounding.
eg:
Round(1.5) = 2
Round(2.5) = 2
Round(3.5) = 4
Round(4.5) = 4
casey.
Are bankers off their onions???
Lottery is a tax on people who are bad at maths
If only mosquitoes sucked fat instead of blood...
To do is to be (Descartes). To be is to do (Sartre). To be do be do (Sinatra)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|