Rounding help

  • Thread starter Thread starter DawnTreader
  • Start date Start date
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?
 
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. \
 
Back
Top