joshld
asked on
Decimal to Fractions
I got an equation that will return numbers with a lot of decimal places. I would like for the user to see something that they can measure out with a tape measure so I would like it to round the the nearest 1/16th.
This is what I get: 38.5624897
This is what I want : 3'-2-9/16"
This is all in a textbox.
This is what I get: 38.5624897
This is what I want : 3'-2-9/16"
This is all in a textbox.
How about something simple like:
Dim valu As Single
Dim feet As Integer
Dim inches As Integer
Dim sixteenths As Integer
Dim txt As String
Dim dec As Single
Dim i As Integer
valu = 38.5624897 'use for testing
Rem *** Get feet ***
feet = valu \ 12
Rem Get inches ***
txt = Format$(valu - feet * 12, "0.0")
i = InStr(txt, ".")
If i Then
inches = Val(Left$(txt, i - 1))
End If
Rem *** Get fraction ***
txt$ = CStr(valu)
i = InStr(txt, ".")
If i Then
Rem *** There are decimal places ***
dec = Val("0" & Mid(txt, i))
sixteenths = dec / 0.0625
End If
Rem *** Compile answer ***
txtValu.Text = CStr(feet) & "'-" & CStr(inches) & "-" & CStr(sixteenths) & "\16"
Dim valu As Single
Dim feet As Integer
Dim inches As Integer
Dim sixteenths As Integer
Dim txt As String
Dim dec As Single
Dim i As Integer
valu = 38.5624897 'use for testing
Rem *** Get feet ***
feet = valu \ 12
Rem Get inches ***
txt = Format$(valu - feet * 12, "0.0")
i = InStr(txt, ".")
If i Then
inches = Val(Left$(txt, i - 1))
End If
Rem *** Get fraction ***
txt$ = CStr(valu)
i = InStr(txt, ".")
If i Then
Rem *** There are decimal places ***
dec = Val("0" & Mid(txt, i))
sixteenths = dec / 0.0625
End If
Rem *** Compile answer ***
txtValu.Text = CStr(feet) & "'-" & CStr(inches) & "-" & CStr(sixteenths) & "\16"
Oops, took too long to type in it and test, got beat. Oh well...
ASKER
I would like for it to be able to simplify the fraction, in other words I would like it to display 2-1/2" if it was 2.5. is there a way to make that work?
OK, clunky, but it works:
Dim valu As Single
Dim feet As Integer
Dim inches As Integer
Dim sixteenths As Integer
Dim txt As String
Dim dec As Single
Dim i As Integer
Dim ans As String
valu = 2.5
Rem *** Get feet ***
feet = valu \ 12
Rem Get inches ***
txt = Format$(valu - feet * 12, "0.0")
i = InStr(txt, ".")
If i Then
inches = Val(Left$(txt, i - 1))
End If
Rem *** Get fraction ***
txt = CStr(valu)
i = InStr(txt, ".")
If i Then
Rem *** There are decimal places ***
dec = Val("0" & Mid(txt, i))
sixteenths = dec / 0.0625
End If
Rem *** Get fraction ***
Select Case sixteenths
Case 2
frac = "1/8"
Case 4
frac = "1/4"
Case 6
frac = "3/8"
Case 8
frac = "1/2"
Case 10
frac = "5/8"
Case 12
frac = "3/4"
Case 14
frac = "7/8"
Case Else
frac = CStr(sixteenths) & "\16"
End Select
Rem *** Compile answer ***
If feet Then
ans = CStr(feet) & "'-"
End If
If inches Then
ans = ans & CStr(inches) & "-"
End If
If Len(frac) Then
ans = ans & frac
End If
Rem *** Display answer ***
txtValu.Text = ans
Dim valu As Single
Dim feet As Integer
Dim inches As Integer
Dim sixteenths As Integer
Dim txt As String
Dim dec As Single
Dim i As Integer
Dim ans As String
valu = 2.5
Rem *** Get feet ***
feet = valu \ 12
Rem Get inches ***
txt = Format$(valu - feet * 12, "0.0")
i = InStr(txt, ".")
If i Then
inches = Val(Left$(txt, i - 1))
End If
Rem *** Get fraction ***
txt = CStr(valu)
i = InStr(txt, ".")
If i Then
Rem *** There are decimal places ***
dec = Val("0" & Mid(txt, i))
sixteenths = dec / 0.0625
End If
Rem *** Get fraction ***
Select Case sixteenths
Case 2
frac = "1/8"
Case 4
frac = "1/4"
Case 6
frac = "3/8"
Case 8
frac = "1/2"
Case 10
frac = "5/8"
Case 12
frac = "3/4"
Case 14
frac = "7/8"
Case Else
frac = CStr(sixteenths) & "\16"
End Select
Rem *** Compile answer ***
If feet Then
ans = CStr(feet) & "'-"
End If
If inches Then
ans = ans & CStr(inches) & "-"
End If
If Len(frac) Then
ans = ans & frac
End If
Rem *** Display answer ***
txtValu.Text = ans
Oops, didn't format out right if there are no sixteenths. This is the final working code:
Dim valu As Single
Dim feet As Integer
Dim inches As Integer
Dim sixteenths As Integer
Dim txt As String
Dim dec As Single
Dim i As Integer
Dim ans As String
valu = 2.7
Rem *** Get feet ***
feet = valu \ 12
Rem Get inches ***
txt = Format$(valu - feet * 12, "0.0")
i = InStr(txt, ".")
If i Then
inches = Val(Left$(txt, i - 1))
End If
Rem *** Get fraction ***
txt = CStr(valu)
i = InStr(txt, ".")
If i Then
Rem *** There are decimal places ***
dec = Val("0" & Mid(txt, i))
sixteenths = dec / 0.0625
End If
Rem *** Get fraction ***
Select Case sixteenths
Case 0
frac = ""
Case 2
frac = "1/8"
Case 4
frac = "1/4"
Case 6
frac = "3/8"
Case 8
frac = "1/2"
Case 10
frac = "5/8"
Case 12
frac = "3/4"
Case 14
frac = "7/8"
Case Else
frac = CStr(sixteenths) & "\16"
End Select
Rem *** Compile answer ***
If feet Then
ans = CStr(feet) & "'-"
End If
If inches Then
ans = ans & CStr(inches)
End If
If Len(frac) Then
ans = ans & "-" & frac
End If
Rem *** Display answer ***
txtValu.Text = ans
Dim valu As Single
Dim feet As Integer
Dim inches As Integer
Dim sixteenths As Integer
Dim txt As String
Dim dec As Single
Dim i As Integer
Dim ans As String
valu = 2.7
Rem *** Get feet ***
feet = valu \ 12
Rem Get inches ***
txt = Format$(valu - feet * 12, "0.0")
i = InStr(txt, ".")
If i Then
inches = Val(Left$(txt, i - 1))
End If
Rem *** Get fraction ***
txt = CStr(valu)
i = InStr(txt, ".")
If i Then
Rem *** There are decimal places ***
dec = Val("0" & Mid(txt, i))
sixteenths = dec / 0.0625
End If
Rem *** Get fraction ***
Select Case sixteenths
Case 0
frac = ""
Case 2
frac = "1/8"
Case 4
frac = "1/4"
Case 6
frac = "3/8"
Case 8
frac = "1/2"
Case 10
frac = "5/8"
Case 12
frac = "3/4"
Case 14
frac = "7/8"
Case Else
frac = CStr(sixteenths) & "\16"
End Select
Rem *** Compile answer ***
If feet Then
ans = CStr(feet) & "'-"
End If
If inches Then
ans = ans & CStr(inches)
End If
If Len(frac) Then
ans = ans & "-" & frac
End If
Rem *** Display answer ***
txtValu.Text = ans
ASKER
Sweet, I will try and implement that tomorrow.
ASKER
I got a question, what are the "$" for after the format and left functions? They are coming up with errors.
Excuse me, I use VB6, where that is how the function is called. For .NET, I believe removing the $'s will make it work.
ASKER
Ok, I removed it but I still have an error that I cannot understand. Sorry. See if the attached image makes any sense.
untitled.bmp
untitled.bmp
I have a solution, will post in about a hour when I get back from lunch.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Darn, screwed it up. I meant:
Same thing for Mid. Replace:
dec = Val("0" & Mid(txt, i))
with either:
s = "0" & Mid(txt, i)
or
s = "0" & txt.Substring(i)
then use:
dec = Val(s)
Same thing for Mid. Replace:
dec = Val("0" & Mid(txt, i))
with either:
s = "0" & Mid(txt, i)
or
s = "0" & txt.Substring(i)
then use:
dec = Val(s)
ASKER
ok, this is what I have so far. Only problem is that it is not displaying the inches, such as you input 38.658 and it returns 3'--11/16" when it should be 3'-2-11/16", any ideas? thanks for the help so far!
Dim s As String
Dim valu As Single
Dim feet As Integer
Dim inches As Integer
Dim sixteenths As Integer
Dim txt As String
Dim dec As Single
Dim i As Integer
Dim ans As String
Dim frac As String
valu = txtA.Text
REM *** Get feet ***
feet = valu \ 12
REM Get inches ***
txt = Format$(valu - feet * 12, "0.0")
i = InStr(txt, ".")
If i Then
s = txt.Substring(1, i - 1)
inches = Val(s)
End If
REM *** Get fraction ***
txt = CStr(valu)
i = InStr(txt, ".")
If i Then
REM *** There are decimal places ***
dec = Val("0" & Mid(txt, i))
sixteenths = dec / 0.0625
End If
REM *** Get fraction ***
Select Case sixteenths
Case 0
frac = ""
Case 2
frac = "1/8"""
Case 4
frac = "1/4"""
Case 6
frac = "3/8"""
Case 8
frac = "1/2"""
Case 10
frac = "5/8"""
Case 12
frac = "3/4"""
Case 14
frac = "7/8"""
Case Else
frac = CStr(sixteenths) & "\16"""
End Select
REM *** Compile answer ***
If feet Then
ans = CStr(feet) & "'-"
End If
If inches Then
ans = ans & CStr(inches)
End If
If Len(frac) Then
ans = ans & "-" & frac
End If
REM *** Display answer ***
MsgBox("Answer = " & ans)
valu = txtA.Text
inch = valu \ 1
fract = valu - inch
feet = valu \ 12
inch = inch Mod 12
fract = Round(fract * 12)
Select Case fract
Case 0
frac = "-"
Case 2
frac = "-1/8"""
Case 4
frac = "-1/4"""
Case 6
frac = "-3/8"""
Case 8
frac = "-1/2"""
Case 10
frac = "-5/8"""
Case 12
frac = "-3/4"""
Case 14
frac = "-7/8"""
Case Else
frac = "-" & CStr(fract) & "/16"""
End Select
ans = feet & "-" & inch & frac
MsgBox "Answer = " & ans
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks, final code:
Dim valu As Single
Dim feet As Integer
Dim inch As Integer
Dim ans As String
Dim fract As String
valu = txtA.Text
inch = valu \ 1
fract = valu - inch
feet = valu \ 12
inch = inch Mod 12
fract = Math.Round(fract * 16)
Select Case fract
Case 0
fract = ""
Case 2
fract = "-1/8"""
Case 4
fract = "-1/4"""
Case 6
fract = "-3/8"""
Case 8
fract = "-1/2"""
Case 10
fract = "-5/8"""
Case 12
fract = "-3/4"""
Case 14
fract = "-7/8"""
Case Else
fract = "-" & CStr(fract) & "/16"""
End Select
ans = feet & "'-" & inch & fract
MsgBox("Answer = " & ans)
Dim valu As Single
Dim feet As Integer
Dim inch As Integer
Dim ans As String
Dim fract As String
valu = txtA.Text
inch = valu \ 1
fract = valu - inch
feet = valu \ 12
inch = inch Mod 12
fract = Math.Round(fract * 16)
Select Case fract
Case 0
fract = ""
Case 2
fract = "-1/8"""
Case 4
fract = "-1/4"""
Case 6
fract = "-3/8"""
Case 8
fract = "-1/2"""
Case 10
fract = "-5/8"""
Case 12
fract = "-3/4"""
Case 14
fract = "-7/8"""
Case Else
fract = "-" & CStr(fract) & "/16"""
End Select
ans = feet & "'-" & inch & fract
MsgBox("Answer = " & ans)
Open in new window