Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

RoundUp to 5 1

Status
Not open for further replies.

lazytrucker

Programmer
Aug 4, 2004
39
0
0
GB
Hi all just wondering if anyone has a script which will round up a number to the nearest 5 eg:

123.15 >> 125.00

128.72 >> 130.00

Any suggestions will be appreciated

Cheers Lazytrucker
 
Just do an integer division by 5, add 1 to the answer, and multiply by 5

________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'If we're supposed to work in Hex, why have we only got A fingers?'

for steam enthusiasts
 
In VBScript:

Dim rounded, unrounded

unrounded = 20.125
rounded = CInt(unrounded + .5)
rounded = rounded + (rounded Mod 5)

Lee
 
johnwm's method works if you want numbers that are evenly divisible by 5 to still round up (125 becomes 130), but if you want those to remain where they are it doesn't work (it will round up to the next 5). If that's cool then it works great.

trollacious's doesn't work for numbers not evenly divisible by 5 after the first step (123.15 + .5 = 123.65, intergerized becomes 123, 123 mod 5 = 3, 123 + 3 = 126).

If you want to round up to the nearest 5 but keep numbers already evenly divisible where they are, off the top of my head a conditional version of johnwm's will work:
Code:
If YourNumber Mod 5 > 0 Then YourNumber = 5 * (CInt(YourNumber / 5) + 1)
With the conditional, it remains unchanged if it's already evenly divisible by 5.

If you were to do it as a function then something like:
Code:
Function RoundUpTo5(NumIn)
    If NumIn Mod 5 > 0 Then NumIn = 5 * (CInt(NumIn / 5) + 1)
    RoundUpTo5 = NumIn
End Function
would work.
 
That looks fair enough but if the number is is 3.00 then it rounds to 10

I have a string manip func that works but it is messy.

::

a = 435.00
b = 433.00
a= cstr(formatnumber(a))
b= cstr(formatnumber(b))

function roundUp(cost)
if formatnumber(right(cost, 4)) <> 0 then
if formatnumber(right(cost, 4)) < 5 then
cost = cstr(left(cost, (len(cost)-4))) & "5.00"
cost = cdbl(cost)
response.write "RoundUP = " & formatnumber(cost) & "<br>"
elseif formatnumber(right(cost, 4)) > 5 then
if len(cost) > 5 then
cost = cstr(left(cost, (len(cost)-5))) & (mid(cost, (len(cost)-4), 1)+1) & "0.00"
response.write "RoundUP = " & formatnumber(cost) & "<br>"
elseif len(cost) <= 5 then
cost = (mid(cost, (len(cost)-4), 1)+1) & "0.00"
response.write "RoundUP = " & formatnumber(cost) & "<br>"
end if
end if
end if
roundUp=cost
end function

c=roundUp(a)
d=roundUp(b)
response.write "<br>" & c & "<br>" & d
 
The function I had is a much faster way to do it. I simply neglected to use Int, and used CInt instead. The former integerizes, the latter rounds to the nearest integer. This works perfectly (tested):
Code:
Function RoundUpTo5(NumIn)
    If NumIn Mod 5 > 0 Then NumIn = 5 * (Int(NumIn / 5) + 1)
    RoundUpTo5 = NumIn
End Function
One letter sure makes a difference. :)
 
Also, if you want it to work with negative numbers, too (rounding UP to the nearest 5, leaving evenly-divisible-by-5 numbers alone) you need to change the comparison from > 0 to <> 0 instead.
 
Handling negative numbers also means that you have to decide whether rounding 'up' means 'close to' or 'farther from' zero. i.e.: does -142 become -140 (close to) or -145 (farther from)
 
Agreed. By "up" I meant to a higher number, not further from 0. For most applications, though, I'd think "further from 0" is a better form of rounding "up".
 

Thanks for all the replies but none of these soloutins work with decimal places.
It is £'s and pence Im working with here so no negatives and always two decimal places.
I am trying to alter genimuse's script but maths was never my strong point.

Any suggestions very welcome.

Cheers lazytrucker.
 
This seems to work funny result for value 0.01 though as it is always rounded down hence the extra statement.

Code:
Function RoundUpTo5(NumIn) 
			If NumIn = 0.01 then
			 NumIn = 5
			 else			 
    		NumIn = round(NumIn + 0.49)        		
        If NumIn Mod 5 > 0 Then 
				  NumIn = 5 * (Int(NumIn / 5) + 1)
    		end if
			end if			
       RoundUpTo5 = NumIn
		End Function

Cheers LazyTrucker
 
Man, that wiley VBScript and its imprecision. Well, since it's willing to believe that 0.01 mod 5 is 0 -- which mathemtatically is simply untrue -- then it's possible that it will fail with other numbers we're not aware of, like (and I haven't tested this one) 100.01. As such we can just change the math so the test is a different one, one that doesn't use mod. Use this instead (change in blue):
Code:
Function RoundUpTo5(NumIn)
    If [COLOR=blue]NumIn <> 5 * Int(NumIn / 5)[/color] Then NumIn = 5 * (Int(NumIn / 5) + 1)
    RoundUpTo5 = NumIn
End Function
That can't fail.

This test:
Code:
Function RoundUpTo5(NumIn)
    If NumIn <> 5 * Int(NumIn / 5) Then NumIn = 5 * (Int(NumIn / 5) + 1)
    RoundUpTo5 = NumIn
End Function

dim x

x = 123.15
Response.Write("<p>" & x & " becomes " & RoundUpTo5(x) & "</p>")

x = 128.72
Response.Write("<p>" & x & " becomes " & RoundUpTo5(x) & "</p>")

x = 3.00
Response.Write("<p>" & x & " becomes " & RoundUpTo5(x) & "</p>")

x = 25
Response.Write("<p>" & x & " becomes " & RoundUpTo5(x) & "</p>")

x = 0.01
Response.Write("<p>" & x & " becomes " & RoundUpTo5(x) & "</p>")
results in this output:
Code:
123.15 becomes 125

128.72 becomes 130

3 becomes 5

25 becomes 25

0.01 becomes 5
which should finally do the trick for you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top