Convert Decimal to Fraction in VB 2008

Public Function Dec2Frac(ByVal f As Double) As String

Dim df As Double
Dim lUpperPart As Long
Dim lLowerPart As Long
Dim intNumerator As Integer
Dim intDenom As Integer

lUpperPart = 1
lLowerPart = 1

df = lUpperPart / lLowerPart
While (df <> f)
If (df < f) Then
lUpperPart = lUpperPart + 1
Else
lLowerPart = lLowerPart + 1
lUpperPart = f * lLowerPart
End If
df = lUpperPart / lLowerPart
End While

intNumerator = Fix(lUpperPart / lLowerPart)
intDenom = lUpperPart Mod lLowerPart

Dec2Frac = intNumerator.ToString & " " & intDenom & "/" & lLowerPart
End Function

Comments