Results 1 to 3 of 3
  1. #1
    Lounger
    Join Date
    Apr 2002
    Posts
    38
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Rounding (Access 2003)

    Hi All
    I want to round a decimal figure UP but only to the nearest .5 ( ie 1.27 would round to 1.5 or 1.75 would round to 2.0)

    Any thoughts please

    Toncc

  2. #2
    5 Star Lounger st3333ve's Avatar
    Join Date
    May 2003
    Location
    Los Angeles, California, USA
    Posts
    705
    Thanks
    0
    Thanked 2 Times in 2 Posts

    Re: Rounding (Access 2003)

    Assuming I can trust my notes, the heart of the following code came from the Utter Access website:
    <pre>Function RoundHalf(sngTarget As Single) As Single

    RoundHalf = RoundToTheNearest(sngTarget, 0.5)

    End Function

    Function RoundToTheNearest(sngTarget As Single, sngDividend As Single) As Single

    Dim dblTarget As Double
    Dim dblDividend As Double
    Dim dblRTN As Double

    dblTarget = CDbl(sngTarget)
    dblDividend = CDbl(sngDividend)

    dblRTN = Int((dblTarget / dblDividend) + 0.5) * dblDividend
    RoundToTheNearest = CSng(dblRTN)

    End Function</pre>


  3. #3
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Rounding (Access 2003)

    If you want to avoid VBA code, you can use the following expression in a query to round a field Amount up to the nearest 0.5:

    Int(([Amount]+0.499999999999999999999)/0.5)*0.5

    As control source of a text box on a form or report, just prefix the expression with =

    =Int(([Amount]+0.499999999999999999999)/0.5)*0.5

    The use of 0.499999999999999999999 instead of 0.5 prevents multiples of 0.5 from being rounded up to the next higher multiple.

Posting Permissions

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