Results 1 to 3 of 3
  1. #1
    Star Lounger
    Join Date
    Jan 2002
    Location
    Washington State
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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. #2
    Platinum Lounger
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    4,594
    Thanks
    0
    Thanked 27 Times in 27 Posts

    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. #3
    Star Lounger
    Join Date
    Jan 2002
    Location
    Washington State
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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
  •