I downloaded and used this code in excel for a long time, Now I trying to use it in vb.net
Option Explicit
Function CInches(Text_string_containing_values_for____Feet_Inches)
Dim vVal As String
Dim i As Integer
Dim vChar As Variant
Dim iFt As Integer
Dim iIn As Integer
Dim iNumerator As Integer
Dim iDenominator As Integer
Dim iTemp As Integer
Dim bLastCharWasSpace As Boolean
vVal = Text_string_containing_values_for____Feet_Inches
If IsNumeric(vVal) Then
CInches = vVal
Exit Function
End If
iTemp = 0
bLastCharWasSpace = False
For i = 1 To Len(vVal)
vChar = Mid(vVal, i, 1)
If IsNumeric(vChar) Then
If bLastCharWasSpace = True And iIn = 0 Then
iIn = iTemp
iTemp = 0
End If
If iTemp = 0 Then
iTemp = vChar
Else
iTemp = iTemp * 10
iTemp = iTemp + vChar
End If
ElseIf vChar = "'" Or vChar = "f" Then
iFt = iTemp
iTemp = 0
ElseIf vChar = "/" Then
iNumerator = iTemp
iTemp = 0
ElseIf vChar = """" Or vChar = "i" Then
If iNumerator > 0 Then
iDenominator = iTemp
iTemp = 0
ElseIf iIn = 0 Then
iIn = iTemp
iTemp = 0
End If
End If
If vChar = " " Then
bLastCharWasSpace = True
Else
bLastCharWasSpace = False
End If
Next i
If iNumerator = 0 And iDenominator = 0 Then iDenominator = 1
CInches = (iFt * 12) + iIn + (iNumerator / iDenominator)
End Function
Function CFeet(Decimal_Inches, Optional Enter_16_32_64_Etc__To_Round_Inches_To__Fraction_Of_Inch)
Dim iNumUnits As Long
Dim iUnit As Double
Dim iFeet As Integer
Dim iInches As Integer
Dim dFraction As Double
Dim sFtSymbol As String
Dim vVal As Variant
Dim vDenominator As Variant
vVal = Decimal_Inches
vDenominator = Enter_16_32_64_Etc__To_Round_Inches_To__Fraction_Of_Inch
If IsMissing(vDenominator) Then
iUnit = 1 / 9999
Else
iUnit = 1 / vDenominator
End If
iNumUnits = (vVal / iUnit)
iFeet = Fix(iNumUnits / (12 / iUnit))
iInches = Fix((iNumUnits Mod (12 / iUnit)) * iUnit)
dFraction = (iNumUnits Mod (1 / iUnit)) * iUnit
If iFeet <> 0 Then sFtSymbol = "'-"
CFeet = Application.Text(iFeet, "##") & sFtSymbol & Application.Text(iInches + dFraction, "# ##/##\""")
End Function
This is the modified code which it doesn’t change much, I know that some of you will say that code it’s out of date, but this code gives me the chance to work between 16,32,64
the first Function works as expected, the second one it’s not, It should convert a decimal 66.25 to something like 5'-6 1/4" but all I’m getting 5'- /6" it’s always rounding to the nearest inch instead of a fraction.
Another sample 66.875 5'-6 7/8" 5'- /7"
I have place 3 textboxes ( txtbox1 input string = 5'-6 7/8" the first function(Cinches) changes it in txtbox2 to 66.875 and last txtbox3 changes it back Wrongly to 5'- /7"
Public Shared Function CFeet(ByVal Decimal_Inches, ByVal Enter_16_32_64_Etc__To_Round_Inches_To__Fraction_Of_Inch)
Dim iNumUnits As Long
Dim iUnit As Double
Dim iFeet As Integer
Dim iInches As Integer
Dim dFraction As Double
Dim sFtSymbol As String
Dim vVal As Object
Dim vDenominator As Object
vVal = Decimal_Inches
vDenominator = Enter_16_32_64_Etc__To_Round_Inches_To__Fraction_Of_Inch
If IsNothing(vDenominator) Then
iUnit = 1 / 9999
Else
iUnit = 1 / vDenominator
End If
iNumUnits = (vVal / iUnit)
iFeet = Fix(iNumUnits / (12 / iUnit))
iInches = Fix((iNumUnits Mod (12 / iUnit)) * iUnit)
dFraction = (iNumUnits Mod (1 / iUnit)) * iUnit
If iFeet <> 0 Then sFtSymbol = "'-"
CFeet = Format(iFeet, "##") & sFtSymbol & Format(iInches + dFraction, "# ##/##\""")
End Function
Thanks in advance for any help provided.