# Thread: Round up to nearest nickel (A2K)

1. ## Round up to nearest nickel (A2K)

Hi everyone!

I'm trying to round up a dollar amount to the nearest nickel.

MS has a KB article (#209996) that explains how to do this. But it doesn't always work. Here's the code for the function they recommend:

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

In the immediate window, if I type in:
?RoundToNearest(10.03, 0.05,up)
I should receive 10.05, right? Nope. I get 10.10

Same problem with 10.04.
(10.01, 10.02, and 10.05 all correctly return 10.05.)

Any ideas?
Thanks!
Sam

2. ## Re: Round up to nearest nickel (A2K)

To round up use:
Result = Int((dblNumber + varRoundAmount) / varRoundAmount) * varRoundAmount

To round down use:
Result = Int((dblNumber - varRoundAmount) / varRoundAmount) * varRoundAmount

3. ## Re: Round up to nearest nickel (A2K)

Perfect!

Thanks, Pat!

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•