VBA: Round up decimal number to 'half' or 'whole'

Posted on 2006-05-02
Last Modified: 2010-08-05

I want to write a function that will round up a number to the nearest 'half' or a 'whole'


3.231 becomes 3
3.251 becomes 3.5
3.670 becomes 3.5
3.780 becomes 4
4.123 becomes 4

Any ideas ?


Question by:SpencerSteel
    LVL 13

    Expert Comment

    This should work:

        MsgBox RoundHalfOrWhole(3.231)
        MsgBox RoundHalfOrWhole(3.251)
        MsgBox RoundHalfOrWhole(3.67)
        MsgBox RoundHalfOrWhole(3.78)
        MsgBox RoundHalfOrWhole(4.123)

    Function RoundHalfOrWhole(Number)

        IntPart = Fix(Abs(Number))
        diff = Abs(Number) - IntPart
        If diff < 0.25 Then
            RoundHalfOrWhole = IntPart
        ElseIf diff >= 0.25 And diff < 0.75 Then
            RoundHalfOrWhole = IntPart + 0.5
            RoundHalfOrWhole = IntPart + 1
        End If
        If Sgn(Number) = -1 Then
            RoundHalfOrWhole = RoundHalfOrWhole * -1
        End If

    End Function
    LVL 76

    Accepted Solution

    Try this function
    Function NearestHalf(n As Single) As Single
        n = CInt(2 * n)
        NearestHalf = n / 2
    End Function
    LVL 17

    Expert Comment

    Private Function rn(number)
    mynum = FormatNumber(number, 3)
    cmp = 1 - (Int(mynum + 1) - mynum)
    If cmp >= 0.75 Then
    rn = mynum + 1 - cmp
    ElseIf cmp < 0.75 And cmp >= 0.25 Then
    rn = mynum - cmp + 0.5
    rn = mynum - cmp
    End If
    End Function
    LVL 17

    Expert Comment

    oh, GrahamSkan provided excellent code there!

    Author Comment

    He did indeed ... it's pretty faultless !

    Cem Truk / Reetiseert: I feel bad about that you went to so much hassle ... if it's any concellation, my solution was going to be quite long-winded like yours but I figured someone would have a much 'smarter' solution.

    I was right :)
    LVL 76

    Expert Comment

    Thanks Spencer and Cem Türk -  I'm too lazy to write anything longer.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
    Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
    Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
    This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now