marku24
asked on
RoundUp in Access VBA
I am trying to round a number (double) up to the next integer. Here is a sample of the results I am looking for:
1.25 rounds to 2
.333 rounds to 1
1.8 rounds to 2
1.111 rounds to 2
and so forth. Anyone have a function to do this?
1.25 rounds to 2
.333 rounds to 1
1.8 rounds to 2
1.111 rounds to 2
and so forth. Anyone have a function to do this?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Of course, this fails:
int(1.000001 +.999999)
= 1
as well as other cases.
This never fails:
-Int(-[YourNumber)
-int(-1.000001)
= 2
Thanks for Gustav (cactus_data) for this trick.
int(1.000001 +.999999)
= 1
as well as other cases.
This never fails:
-Int(-[YourNumber)
-int(-1.000001)
= 2
Thanks for Gustav (cactus_data) for this trick.
Great trick to round down a negative.
This also works. You must force the data type for teh math.
? int(CDbl(1.000001 +.999999))
2
This also works. You must force the data type for teh math.
? int(CDbl(1.000001 +.999999))
2
ASKER
thank you very much.
Fails:
int(CDbl(1.0000000001 +.999999))
=1
:marku24:
if you want accuracy, use this to Round up:
-Int(-[YourNumber])
Round down:
Int(YourNumber)
mx
int(CDbl(1.0000000001 +.999999))
=1
:marku24:
if you want accuracy, use this to Round up:
-Int(-[YourNumber])
Round down:
Int(YourNumber)
mx
I agree that -Int(-[YourNumber]) eliminates the chances for errors in accuracy. It should be the preferred method.
And it works for negative numbers!
And it works for negative numbers!
? -int(-(-1.1))
-1
ASKER
will do. I am looking for rounding up only and will adjust accordingly.
Thanks Joe!
I should add that I can't take the credit, I'm rather the messenger. The method is as old as BASIC itself.
/gustav
I should add that I can't take the credit, I'm rather the messenger. The method is as old as BASIC itself.
/gustav
ASKER
thanks