Function MeterToFeet(inmeters As Double, Optional multiplx As Variant) As String
'*******************************************
'Name: MeterToFeet (Function)
'Purpose: Converts meters to feet, inches and
' fractions of inches
'Calls: Function ZFraction2()
'To Test: From debug window: ? MeterToFeet(15)
'Output: 49', 2-35/64"
'*******************************************
Const conMetToInches As Double = 39.3700787401575
Dim feet As Single, inches As Single, fractionx As Double
Dim itemhold As String
multiplx = IIf(IsMissing(multiplx) = True, 64, multiplx)
inches = inmeters * conMetToInches
feet = Int(inches / 12)
inches = inches - (feet * 12)
fractionx = inches - Int(inches)
inches = Int(inches)
MeterToFeet = LTrim(Str(feet)) & "', " & LTrim(Str(inches))
'Note that you may specify the degree of accuracy in the next line.
MeterToFeet = MeterToFeet & "-" & zfraction2(fractionx, multiplx) & """"
End Function
Function zfraction2(zdec As Double, Optional multiplx As Variant) As String
'*******************************************
'Name: zfraction2 (Function)
'Purpose: Converts input into a fraction
' accurate to the nearest multiplx
'Created by: raskew
'Inputs: From debug window: ? zfraction2(.874, 64)
' Default if multiplx not specified = 64
'Output: 0.874=7/8
'*******************************************
Dim Num As Integer, denom As Integer, i As Integer
Dim j As Integer, AtWork As Boolean
'create a fraction, expressed in 64ths
'num = numerator
multiplx = IIf(IsMissing(multiplx) = True, 64, multiplx)
Num = CInt(multiplx * zdec) 'the CInt function rounds up or down
'denom = denominator
denom = multiplx
'reduce the fraction
AtWork = True
Do
j = Num
For i = j To 2 Step -1
If Num Mod i = 0 And denom Mod i = 0 Then
Num = Num / i
denom = denom / i
j = Num
Exit For
End If
Next i
AtWork = False
Loop While AtWork = True
'express the results as a string
zfraction2 = LTrim(Str(Num)) & "/" & LTrim(Str(denom))
End Function