Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.67/5 (3 votes)
See more:
I downloaded and used this code in excel for a long time, Now I trying to use it in vb.net
VB
Option Explicit


'\ This function converts a string like 5'-6 1/4" to a decimal number
'\ of inches that can be used in calculation.
Function CInches(Text_string_containing_values_for____Feet_Inches)

    '\ These values are used to examine the input string, one character at a time
    Dim vVal As String  '\ shorter name for input string
    Dim i As Integer    '\ counter to step through each character in input string
    Dim vChar As Variant '\ temporary storage of each input string character
    
    '\ These variables hold the values we discover for feet, inches and the
    '\ numerator and denominator of the fractional inches
    Dim iFt As Integer  '\ used to store number of feet
    Dim iIn As Integer  '\ number of inches
    Dim iNumerator As Integer   '\ numerator of fractional inches
    Dim iDenominator As Integer '\ denominator of fractional inches
    
    '\ In the process of discovering values for feet and inches, these variable
    '\ are used to accumulate and hold numeric values
    Dim iTemp As Integer    '\ Used to build a number as each digit is read
    
    '\ We want to ignore spaces, except for the very important space between
    '\ the number of inches and the numerator of the fractional inches
    '\ This variable is true if the last character processed was a space
    Dim bLastCharWasSpace As Boolean
   
    '\ First we assign input string to variable with shorter name
    vVal = Text_string_containing_values_for____Feet_Inches
    
    '\ If input string is numeric, then we don't want to convert it
    If IsNumeric(vVal) Then
        CInches = vVal
        Exit Function
    End If
    
    '\ Now we step through each character in input string from left to right
    iTemp = 0
    bLastCharWasSpace = False
    For i = 1 To Len(vVal)
        vChar = Mid(vVal, i, 1)
        
        '\ If character is a number, then we combine it with numbers before it
        '\ to get a number that we can assign to feet, inches, numerator or denominator
        If IsNumeric(vChar) Then
        
            '\ If this is a number and the last character was a space then
            '\ chances are, the number in iTemp is inches and we need to
            '\ start over building the numerator of fractional inches
            If bLastCharWasSpace = True And iIn = 0 Then
                    iIn = iTemp
                    iTemp = 0
            End If
            
            '\ As we read number from left to right, we multiply value of previous
            '\ number (if any) by 10 and add this number
            If iTemp = 0 Then
                iTemp = vChar
            Else
                iTemp = iTemp * 10
                iTemp = iTemp + vChar
            End If
        
        '\ The number we've been buiding must be feet
        ElseIf vChar = "'" Or vChar = "f" Then
            iFt = iTemp
            iTemp = 0
        
        '\ The number we've been bulding must be the numerator of fraction
        ElseIf vChar = "/" Then
            iNumerator = iTemp
            iTemp = 0
            
        '\ The number we've been building must be inches or
        '\ the denominator of the fraction, so we check to see if
        '\ there is a numerator
        ElseIf vChar = """" Or vChar = "i" Then
            If iNumerator > 0 Then
                iDenominator = iTemp
                iTemp = 0
            '\ If no numerator, then the number must be inches
            ElseIf iIn = 0 Then
                iIn = iTemp
                iTemp = 0
             End If
        End If
        
        '\ Now we set our indicator so that when we process the next
        '\ character, we will know if the last character was a space
        If vChar = " " Then
            bLastCharWasSpace = True
        Else
            bLastCharWasSpace = False
        End If
    Next i
    
    '\ To avoid dividing by zero if there was no numerator for fraction,
    '\ we set denominator to 1
    If iNumerator = 0 And iDenominator = 0 Then iDenominator = 1
    
    '\ Finally, we calculate number of decimal inches and return value
    CInches = (iFt * 12) + iIn + (iNumerator / iDenominator)
End Function


'\ This function converts a decimal number of inches to a text string like 5'-6 1/2"
Function CFeet(Decimal_Inches, Optional Enter_16_32_64_Etc__To_Round_Inches_To__Fraction_Of_Inch)
    '\ These variables are used to convert the decimal inches to the number
    '\ of fractional units. For example 6" would convert to 96 16ths
    Dim iNumUnits As Long '\ converted value   = 96 in example
    Dim iUnit As Double   '\ unit used to convert  = 1/16 in example
    
    '\ These varibles are used to hold calculated values that will become
    '\ part of the text string
    Dim iFeet As Integer
    Dim iInches As Integer
    Dim dFraction As Double
    Dim sFtSymbol As String
    
    '\ These variables are used to assign shorter names to input values
    Dim vVal As Variant
    Dim vDenominator As Variant
    
    '\ First we assign shorter names
    vVal = Decimal_Inches
    vDenominator = Enter_16_32_64_Etc__To_Round_Inches_To__Fraction_Of_Inch
    
    '\ If no denominator value was supplied, we will round to 1/9999 of inch
    If IsMissing(vDenominator) Then
        iUnit = 1 / 9999
    Else
        iUnit = 1 / vDenominator
    End If
    
    '\ Now we calculate the number of fractional units in the input value
    '\ Example 6 inches = 96 16ths
    iNumUnits = (vVal / iUnit)
    
    '\ We prepare each part of text string
    iFeet = Fix(iNumUnits / (12 / iUnit))
    iInches = Fix((iNumUnits Mod (12 / iUnit)) * iUnit)
    dFraction = (iNumUnits Mod (1 / iUnit)) * iUnit
    If iFeet <> 0 Then sFtSymbol = "'-"
    
    '\ Finally we format and return text string
    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"


VB
'\ This function converts a decimal number of inches to a text string like 5'-6 1/2"
Public Shared Function CFeet(ByVal Decimal_Inches, ByVal Enter_16_32_64_Etc__To_Round_Inches_To__Fraction_Of_Inch)
    '\ These variables are used to convert the decimal inches to the number
    '\ of fractional units. For example 6" would convert to 96 16ths
    Dim iNumUnits As Long '\ converted value   = 96 in example
    Dim iUnit As Double   '\ unit used to convert  = 1/16 in example

    '\ These varibles are used to hold calculated values that will become
    '\ part of the text string
    Dim iFeet As Integer
    Dim iInches As Integer
    Dim dFraction As Double
    Dim sFtSymbol As String

    '\ These variables are used to assign shorter names to input values
    Dim vVal As Object
    Dim vDenominator As Object

    '\ First we assign shorter names
    vVal = Decimal_Inches
    vDenominator = Enter_16_32_64_Etc__To_Round_Inches_To__Fraction_Of_Inch

    '\ If no denominator value was supplied, we will round to 1/9999 of inch
    If IsNothing(vDenominator) Then
        iUnit = 1 / 9999
    Else
        iUnit = 1 / vDenominator
    End If

    '\ Now we calculate the number of fractional units in the input value
    '\ Example 6 inches = 96 16ths
    iNumUnits = (vVal / iUnit)

    '\ We prepare each part of text string
    iFeet = Fix(iNumUnits / (12 / iUnit))
    iInches = Fix((iNumUnits Mod (12 / iUnit)) * iUnit)
    dFraction = (iNumUnits Mod (1 / iUnit)) * iUnit
    If iFeet <> 0 Then sFtSymbol = "'-"

    '\ Finally we format and return text string
    CFeet = Format(iFeet, "##") & sFtSymbol & Format(iInches + dFraction, "# ##/##\""")

End Function

Thanks in advance for any help provided.
Posted
Comments
Sergey Alexandrovich Kryukov 10-Apr-12 13:32pm    
This is not a valid question. Why do we need to see some unrelated code you've downloaded. Code is normally written, not borrowed and magically adjusted. Start from what do you want to achieve. What's the problem. Ask a distinct question.
--SA
Lukann 10-Apr-12 21:23pm    
Thanks for your great help is an unforgettable one, you can consider yourself a great help to Code project.com
Sergey Alexandrovich Kryukov 12-Apr-12 12:46pm    
You noticed that, too? Thank you very much.
Good luck, call again.
--SA
wizardzz 10-Apr-12 13:34pm    
I'm with SAK, what are you expecting? Free custom modified code?
Lukann 10-Apr-12 21:24pm    
you to mr.W are of great help to the Code Project

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900