AnsweredAssumed Answered

Convert Decimal value to Fractions

Question asked by JOHN GEORGE on Mar 3, 2017
Latest reply on Mar 3, 2017 by Elmar Klammer

I have a macro to convert decimals to fractions and below is the function code

This returns the value from decimal to fraction as shown:

0.984 x 4.724 x 5.512   to  0'-0-63/64" x 0'-4-47/64" x 0'-5-33/64"

Can someone please help me to modify this code so that if "feet" value is less than 1 I would like to eliminate the 0' and show only inches to show 0-63/64" x 4-47/64" x 5-33/64"

Thanks

 

Function DecimalToFeetInches(DecimalLength As Variant, Denominator As Integer) As String
  ' converts decimal inches to feet/inches/fractions
  Dim intFeet As Integer
  Dim intInches As Integer
  Dim intFractions As Integer
  Dim FractToDecimal As Double
  Dim remainder As Double
  Dim tmpVal As Double
  
  ' compute whole feet
  intFeet = Int(DecimalLength / 12)
  remainder = DecimalLength - (intFeet * 12)
  tmpVal = CDbl(Denominator)

  intInches = Int(remainder)
  remainder = remainder - intInches

  If Not (remainder = 0) Then
    If Not (Denominator = 0) Then
      FractToDecimal = 1 / tmpVal
        If FractToDecimal > 0 Then
          intFractions = Int(remainder / FractToDecimal)
          If (remainder / FractToDecimal) - intFractions > 0 Then
            intFractions = intFractions + 1
          End If
        End If
     End If
  End If
  Call FractUp(intFeet, intInches, intFractions, Denominator) ' Simplify up & down
  DecimalToFeetInches = LTrim$(Str$(intFeet)) & "'-"
  DecimalToFeetInches = DecimalToFeetInches & LTrim$(Str$(intInches))
  If intFractions > 0 Then
    DecimalToFeetInches = DecimalToFeetInches & "-"
    DecimalToFeetInches = DecimalToFeetInches & LTrim$(Str$(intFractions))
    DecimalToFeetInches = DecimalToFeetInches & "/" & LTrim$(Str$(Denominator))
  End If   
  DecimalToFeetInches = DecimalToFeetInches & Chr$(34)    
End Function

Outcomes