FeetString

From Microstation VBA Wiki
Jump to: navigation, search

This function takes a value in decimal feet and returns a string with feet, inches, and eighths (can be modified to other fractions), e.g. 4'-3 3/8". In Microstation the fraction is automatically converted to look like a fraction. If there are no feet, the feet part is left off and just inches and a fraction are shown, e.g. 4 1/4".

Function FeetString(dDim As Double) As String

    Dim denom As Double     'level of precision for fractions (8 for 1/8)
    Dim nbrft As Double     'whole number of feet
    Dim inchin As Double    'decimal number of inches
    Dim nbrin As Double     'whole number of inches
    Dim fracin As Double    'decimal number of eighths (or whatever fractions it is)
    Dim numer As Double     'numerator in fraction
    Dim frac As String      'string to hold the fraction
    
    denom = 8                           'must be 2, 4, 8, 16, 32, 64, etc.
    nbrft = Int(dDim)                   'whole number of feet
    inchin = 12 * (dDim - nbrft)        'fractional number of inches
    nbrin = Int(inchin)                 'whole number of inches
    fracin = denom * (inchin - nbrin)
    numer = Round(fracin)
    If numer = 0 Then                   'if numerator is 0, no fraction
        frac = ""
    ElseIf numer = denom Then           'if numerator = denominator, it is 1 inch
        nbrin = nbrin + 1
        frac = ""
    Else
        Do                              'loop to reduce fractions while numerator is even
            If numer / 2 = Int(numer / 2) Then
                numer = numer / 2
                denom = denom / 2
            Else
                frac = " " & Format(numer) & "/" & Format(denom)
                Exit Do
            End If
        Loop
    End If
    FeetString = Format(nbrft) & "'-"
    If nbrft = 0 Then strDim = ""
    FeetString = strDim & Format(nbrin) & frac & """"

End Function