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.

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

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

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

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)

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

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

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial