# 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).

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
``````
LVL 1
###### Who is Participating?

x
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.

Older 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.
Older 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
``````

Experts Exchange Solution brought to you by

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

Mechanical 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
``````
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.
Author 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.
Author 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.
Older 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
Author 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..
Mechanical 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)
``````

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