We help IT Professionals succeed at work.

Decimal to Fractions

joshld
joshld asked
on
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.
Comment
Watch Question

Well, here is the logic: (spare me if syntax is wrong, since I'm not running VB here)
val = 38.5624897;

inch = floor(val)             ' Gives 38
fract = val - inch            ' Gives 0.56...
feet = floor(inch / 12)       ' Gives 3
inch = inch Mod 12            ' Gives 2
fract = round(fract * 16)     ' Gives 9

MsgBox(feet & "-" & inch & "-" & fract & "/16")  ' Displays 3-2-9/16

Open in new window

Top Expert 2009

Commented:
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"
 
Top Expert 2009

Commented:
Oops, took too long to type in it and test, got beat. Oh well...

Author

Commented:
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?
Top Expert 2009

Commented:
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
 
Top Expert 2009

Commented:
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
 

Author

Commented:
Sweet, I will try and implement that tomorrow.

Author

Commented:
I got a question, what are the "$" for after the format and left functions? They are coming up with errors.
Top Expert 2009

Commented:
Excuse me, I use VB6, where that is how the function is called. For .NET, I believe removing the $'s will make it work.

Author

Commented:
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
Top Expert 2009

Commented:
I have a solution, will post in about a hour when I get back from lunch.
Top Expert 2009
Commented:
For both these methods, add another declare statement at the top of the code:
Dim s As String
Now, .NET likes the left side of a Left, Mid, or Right function to be assigned as a string. The lines in my code don't technically do that directly, they're fed to the Val function. So, try this first:
Replace:
inches = Val(Left$(txt, i - 1))

with :
s = Left(txt, i - 1)
inches = Val(s)
 
If that doesn't work, use a .NET-specific function:
s = txt.Substring(1, i - 1)
inches = Val(s)
 
 
Same thing for Mid. Replace:
dec = Val("0" & Mid(txt, i))
with either:
s = "0" & Mid(txt, i - 1)
or
s = "0" & txt.Substring(1, i - 1)
then use:
dec = Val(s)
Top Expert 2009

Commented:
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)

Author

Commented:
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)

Open in new window


       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

Open in new window

OOPS.. 7th line should read
       fract = Round(fract * 16)

and case 0 should be ""
       valu = txtA.Text

       inch = valu \ 1
       fract = valu - inch
       feet = valu \ 12
       inch = inch Mod 12
       fract = Round(fract * 16)

       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

Open in new window

Author

Commented:
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)