# Converting Fractions to Decimals

Using AC2002.

I am using the following code to convert fractions to decimals. However, it won't convert the number if it is a whole number only. Need help in modification.

Private Sub Command1_Click()
Dim x As Integer
Dim y As Integer
Dim z As String
Dim a1 As Integer
Dim a2 As Integer
Dim MyNumber As String

DoCmd.GoToControl "Text1"

x = InStr(1, Text1.Text, " ")
y = InStr(x, Text1.Text, "/")
z = Right\$(Text1.Text, Len(Text1.Text) - x)
y = InStr(1, z, "/")
a1 = Val(Left\$(z, y - 1))
a2 = Val(Right\$(z, Len(z) - y))
MyNumber = Val(Left\$(Text1.Text, x - 1)) + a1 / a2
Text1.Text = MyNumber
End Sub
###### 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.

Commented:

> I am using the following code to convert fractions to decimals. However, it
> won't convert the number if it is a whole number only. Need help in modification.

If it is a whole number only, then your value will be missing a "/".  So, check for y = 0, and if it is, then just use the whole value.

Private Sub Command1_Click()
Dim x As Integer
Dim y As Integer
Dim z As String
Dim a1 As Integer
Dim a2 As Integer
Dim MyNumber As String

DoCmd.GoToControl "Text1"

x = InStr(1, Text1.Text, " ")
y = InStr(x, Text1.Text, "/")
If ( y > 0 ) Then
z = Right\$(Text1.Text, Len(Text1.Text) - x)
y = InStr(1, z, "/")
a1 = Val(Left\$(z, y - 1))
a2 = Val(Right\$(z, Len(z) - y))
MyNumber = Val(Left\$(Text1.Text, x - 1)) + a1 / a2
Else
MyNumber = Val(Text1.Text)
End IF

Text1.Text = MyNumber
End Sub

Hope That Helps,
Dex*
0
Commented:
Try using the Eval function just to perform the division.  For Example:

Dim FractionString as string
Dim DecimalValue as Double

FractionString = "5/8"
DecimalValue = Eval(FractionString)

0
Author Commented:
I inserted your revised code, following are the results:

13 5/8 became 13.625 (works fine)

13 became a Runtime error '5', invalid procedure call and points to this line in the code:
y = InStr(x, Text1.Text, "/")

and when I comment out that line, then the results become:

13 5/8 became 135

13 became 13
0
Commented:
After further refinement, including rthomsen's suggestion, here is what I came up with:

Private Sub Command1_Click()
Dim nPos As Integer
Dim strValue as String
Dim MyNumber As String
Dim nValue As Double

DoCmd.GoToControl "Text1"
strValue = Text1.Text
nPos = InStr( strValue, " " )
If ( nPos > 0 ) Then
nValue = CLng(Left(strValue, nPos-1))
strValue = Mid(strValue, nPos+1)
Else
nValue = 0
End If
nValue = nValue + Eval( strValue )
MyNumber = CStr(nValue)
Text1.Text = MyNumber
End Sub

If that doesn't work for you, let me know what errors / problems you have.  Thanks.

Dex*
0
Commented:
The updated version should fix the issues that you just reported.  Try it out and let me know.

Dex*
0

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.

Author Commented:
Worked just fine. Thanks!
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.