Forum Moderators: open

Message Too Old, No Replies

Rounding UP

How to always round up

         

dotme

4:43 pm on Oct 21, 2004 (gmt 0)

10+ Year Member



I have a client who needs his fee rounded up. The VB round() is smart enough to round up or down, so

4.236 = 4.24
4.231 = 4.23

But... he wants 4.231 to also round UP to the nearest penny (=4.24)

Anyone have a code snippet to share? Thanks :-)

txbakers

5:06 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



not sure of the syntax but here it is:

amt = 4.321

if amt mod cint(amt) > 0 then
amt = cint(amt) + 1
end if

will round up to the next integer.

I imagine you could do the math to lower decimals as well. Just check to see if the modulus is > 0, and if so, always round up.

dotme

5:36 pm on Oct 21, 2004 (gmt 0)

10+ Year Member



Well, that didn't work quite as I expected, but when I searched on some key text in your response, I found a fix. I'm posting the solution I found below in case it's useful for anyone else.

Thanks for helping me out!

DotMe

amt = 4.2423432
amt = amt * 100
amt = roundup(amt)
amt = amt / 100
response.write amt

Function roundup(Num)
RoundUp = -Int(-Num)
End Function

plumsauce

6:21 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Back in the cobol days, the solution was:

(round(n*100+0.5))/100

mattglet

7:39 pm on Oct 21, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adding .5 was always my preferred method as well.