Back

Decimal to Fraction

'Deci to Frac



Option Explicit



Private Sub Command1_Click()

Dim upperPart As Long

Dim lowerPart As Long



Call Dec2Frac(Val(Text1.Text) / Val(Text2.Text), upperPart, lowerPart)

Text3.Text = upperPart & "/" & lowerPart

End Sub



Private Sub Dec2Frac(ByVal f As Double, upperPart As Long, lowerPart As Long)

Dim df As Double

upperPart = 1

lowerPart = 1



df = upperPart / lowerPart

While (df <> f)

If (df < f) Then

upperPart = upperPart + 1

Else

lowerPart = lowerPart + 1

upperPart = f * lowerPart

End If

df = upperPart / lowerPart

Wend

End Sub