Unit of Measure conversion (1 Viewer)

J

john

Guest
I am looking for code that will convert decimals to inches. Ex .125 = 1/8
Any help would be appreciated.
 
A

ahenduck

Guest
It sounds like a job for a very simple function. View the code below.

Function DecimalFraction(num As Double, denominator As Long) As String
Dim numerator As Long
Dim tempstr As String
Dim tempnum As Double

If num >= 1 Then
tempstr = Trim(str(Int(num)))
tempnum = num - Int(num)
Else
tempstr = ""
tempnum = num
End If
numerator = Int(denominator * tempnum)
If numerator > 0 And tempstr = "" Then
DecimalFraction = Trim(str(numerator)) + "/" + Trim(str(denominator))
ElseIf numerator = 0 And tempstr = "" Then
DecimalFraction = "0"
ElseIf numerator = 0 And tempstr <> "" Then
DecimalFraction = tempstr
ElseIf numerator > 0 And tempstr <> "" Then
DecimalFraction = tempstr + " " + Trim(str(numerator)) + "/" + Trim(str(denominator))
End If

End Function

This code is not elegant, but will give a fraction based on the denominator given.
 

Users who are viewing this thread

Top Bottom