Help with function that deals with rounding down if not evenly divisable by 4.

I'm trying to complete a function that states... If number is not evenly divisible by 4 (with 2 decimal points), round down to nearest number that is divisible by 4 (with 2 decimal points).

Please note the following code in answering the question above.

When I do a Print FixDivByFour(10.21) it gives the correct answer as 10.20
If I do a Print FixDivByFour(10.20) it gives me 0.... it should return 10.20

What am I doing wrong?

Public Function FixDivByFour(EmployeeShareValue As Variant) As Double

    Dim strStartValue As String
    Dim arrValue() As String
    Dim strCents As String
    Dim EmployeeShare As Double

    If IsNumeric(EmployeeShareValue) Then
    
        EmployeeShare = EmployeeShareValue
    
        strStartValue = EmployeeShare / 4
        arrValue = Split(strStartValue, ".")
        
        If Len(arrValue(1)) <= 2 Then
            
        Else
            Do Until Len(arrValue(1)) <= 2
                EmployeeShare = EmployeeShare - 0.01
                strStartValue = EmployeeShare / 4
                arrValue = Split(strStartValue, ".")
            Loop
            
            FixDivByFour = EmployeeShare
        End If
    
    End If

    
    

End Function

Open in new window

LVL 1
stephenlecomptejrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Martin LissOlder than dirtCommented:
Please give a couple of examples of a number that is "evenly divisible by 4 (with 2 decimal points)" and one that's not.
0
Martin LissOlder than dirtCommented:
Try this.

Function FixDivByFour(EmployeeShareValue) As Double

Dim dblWork As Double
Dim lngRightPart As Long

If IsNumeric(EmployeeShareValue) Then
    Do Until lngRightPart <= 99 And lngRightPart > 0
        dblWork = EmployeeShareValue / 4
        lngRightPart = Split(CStr(dblWork), ".")(1)
        If lngRightPart <= 99 Then
            Exit Do
        End If
        EmployeeShareValue = EmployeeShareValue - 0.01
    Loop
End If

FixDivByFour = EmployeeShareValue
End Function

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
byundtMechanical EngineerCommented:
If EmployeeShareValue is calculated (e.g. 10.2395), then you may encounter an overflow error.

I overcame that by multiplying by the original number by 25, adding a small decimal fraction and truncating the result. Dividing the result by 25 is equivalent to rounding the original number down to a multiple of 0.04.
Function FixDivByFour(EmployeeShareValue) As Double
Dim d As Double
If IsNumeric(EmployeeShareValue) Then
    d = EmployeeShareValue * 25 + 0.0000001
    FixDivByFour = Int(d) / 25
End If
End Function

Open in new window

Does your function need to handle negative numbers as well as positive ones? Note that the suggested code rounds negative numbers down, so -10.23 becomes -10.24. If you instead want to round negative numbers up, then a logic tweak is required.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

stephenlecomptejrAuthor Commented:
These are great comments that help me out greatly.  I'm trying to help a friend with this function and getting their input.  Will respond as quickly as possible.
0
stephenlecomptejrAuthor Commented:
The first one works well since data will never exceed two decimals and will not be negative.   I appreciate all the helpful and well-thought out replies.
0
Martin LissOlder than dirtCommented:
You're welcome and I'm glad I was able to help.

In my profile you'll find links to some articles I've written that may interest you.
Marty - MVP 2009 to 2015, Experts-Exchange Top Expert Visual Basic Classic 2012 to 2014
0
stephenlecomptejrAuthor Commented:
Hi Martin, one thing a user noticed with this function that if 0 is entered - it does not change the value... can you please check on that one?  thanks..
0
byundtMechanical EngineerCommented:
The function you are trying to use errors out on the following statement because there is no decimal point in a 0 (so Split only returns one substring):
lngRightPart = Split(CStr(dblWork), ".")(1)

Open in new window


My suggested code will work as expected, however, and returns 0.

Brad
0
byundtMechanical EngineerCommented:
After further testing, I believe the code in the Answer will give wrong answers for any whole number that is a multiple of 4 (e.g. 4.00, 8.00, 12.00). Even more vexing, it gives the wrong answers for 21 different number of cents (e.g. $0.03, $0.15 and $0.23). Those same 21 different number of cents  plus $4 also return wrong answers (e.g. $4.03, $4.15 and $4.23). Ditto with $8, $12, etc.

See the attached workbook for a test at all increments of 1 cent between 0 and 14.09. I filtered the worksheet so it displays only those rows where the code in the Answer returns incorrect values.

I was originally going to suggest a tweak to the code to handle the problem of 0. I don't see a simple change that can handle all the other incorrect values, however.

The Assisted Solution code, however, gives the desired results for all the inputs.
RoundTo4CentsQ28713863.xlsm
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Access

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.