by Waty Thierry (60 Submissions)
Category: Math/Dates
Compatability: Visual Basic 3.0
Difficulty: Unknown Difficulty
Originally Published: Tue 13th April 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)
Decimal to fraction
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