round up to nearest

stevo
05-31-2004, 03:40 AM
i am dealing with currency variables and what i want to do is always round up to the nearest 10p.

if it was 33.42 then i want it to return 33.50 but if its already on a full 10p like 33.40 then do nothing.

thanks.

ShaneH
05-31-2004, 03:43 AM
Check out the Round function

stevo
05-31-2004, 03:47 AM
i have but it rounds up and down depending on the number.

thanks

ShaneH
05-31-2004, 03:51 AM
Well, you can roll your own Round function, then, or do a simple hack like this:


If nNumber - int(nNumber) < .05 Then
nNumber = nNumber + .05
End If
nNumber = Round(nNumber, 2)

thingimijig
05-31-2004, 03:55 AM
this is a neat little trick to always round up

yournumber = -Int(-yournumber)

so, because you are rounding up the decimal part, try this.

yournumber = -Int(-yournumber * 10) / 10


thingimijig.

stevo
05-31-2004, 04:07 AM
thats it !!, thank you.

bluelotus
05-31-2004, 04:22 AM
i am dealing with currency variables and what i want to do is always round up to the nearest 10p.

if it was 33.42 then i want it to return 33.50 but if its already on a full 10p like 33.40 then do nothing.

thanks.
just another method....


If (CDbl(yourNumber) - CInt(yourNumber)) <> 0 Then
If ((CDbl(yourNumber) - CInt(yourNumber)) * 100 Mod 10) > 0 Then
yourNumber = CInt(yourNumber) + 1
End If
End If


all the best....
:)

darkforcesjedi
05-31-2004, 08:47 AM
Why not just

number = Int(10*(number + 0.5)) / 10

stevo
05-31-2004, 09:22 AM
darkforcesjedi, even though i am happy with what i have, i tried yours and

33.42 returns 33.9

thanks anyway.

darkforcesjedi
05-31-2004, 11:23 AM
Oops, thats because I put a parenthesis in the wrong place

number = Int(10*(number) + 0.5) / 10

but then again you wanted to round UP and not just round the number so nevermind anyway.

bluelotus
05-31-2004, 10:28 PM
just another method....


If (CDbl(yourNumber) - CInt(yourNumber)) <> 0 Then
If ((CDbl(yourNumber) - CInt(yourNumber)) * 100 Mod 10) > 0 Then
yourNumber = CInt(yourNumber) + 1
End If
End If


all the best....
:)
oops... there was a small mistake in my previous code... it wont satisfy the purpose..
below given is the updated code... i know that u have got better solution... just correcting my mistake...


If (CDbl(yourNumber) - CInt(yourNumber)) <> 0 Then
If ((CDbl(yourNumber) - CInt(yourNumber)) * 100 Mod 10) > 0 Then
yourNumber = CDbl(yourNumber) - ((((CDbl(yourNumber) - CInt(yourNumber)) * 100) Mod 10) / 100) + 0.1
End If
End If


thanx..
:)

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum