Rounding help

D

DawnTreader

Hello All

Is there a way to make this code round differently?

Function RoundToNearest(dblNumber As Double, varRoundAmount As Double,
Optional varUp As Variant) As Double

Dim dblTemp As Double
Dim lngTemp As Long

dblTemp = dblNumber / varRoundAmount
lngTemp = CLng(dblTemp)

If lngTemp = dblTemp Then
RoundToNearest = dblNumber
Else
If IsMissing(varUp) Then
' round down
dblTemp = lngTemp
Else
' round up
dblTemp = lngTemp + 1

End If
RoundToNearest = dblTemp * varRoundAmount
End If
End Function

i have a situation where i want to round to the nearest 1000's. that is
easy, but i want to be able to specify the point at which it rounds up to the
1000. for instance i want to round by 1000's but at 750 i want the code to
consider it a 1000. currently it does this at 500. once my data hits
500.00..etc..1 it becomes a thousand. i want to be able to "pad" it so that i
can use it as a warning system.

for instance the system i am working on records the running hours of a
product and i need to service them at intervals, i want to know ahead of the
1000 hour interval that it is coming up on that interval, thus the padding.
should i put in another function variable for the amount to pad?
 
A

Allen Browne

So, if it's within 250 of the 1000, you want to bump it up?

Add 250, and then use integer division:
(dblNumber + 250) \ 1000

The integer divisor operator is the opposite slash, i.e. \
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Calling a function 2
roundup() 0
roundup() function 2
Help with This Expression 2
RoundToNearest 2
Rounding Up 3
Set as Default Template not working 0
Uisng Assignments.Add and the effect on units. 12

Top