Function dec2frac(ByVal dblDecimal As Double) As String
Dim intNumerator, intDenominator, intNegative As Integer
Dim dblFraction, dblAccuracy As Double
Dim txtDecimal As String
dblAccuracy = 0.1
txtDecimal = dblDecimal.ToString
For i As Integer = 0 To (txtDecimal.Length - 1)
If txtDecimal.Substring(i, 1) = "." Then
dblAccuracy = 1 / 10 ^ (txtDecimal.Length - i)
Exit For
End If
Next
intNumerator = 0
intDenominator = 1
intNegative = 1
If dblDecimal < 0 Then
intNegative = -1
End If
dblFraction = 0
Do While Math.Abs(dblFraction - dblDecimal) > dblAccuracy
If Math.Abs(dblFraction) > Math.Abs(dblDecimal) Then
intDenominator += 1
Else
intNumerator += intNegative
End If
dblFraction = intNumerator / intDenominator
Loop
Return intNumerator.ToString & "/" & intDenominator.ToString
End Function
Function num(ByVal dblDecimal As Double) As String
Dim intNumerator, intDenominator, intNegative As Integer
Dim dblFraction, dblAccuracy As Double
Dim txtDecimal As String
dblAccuracy = 0.1
txtDecimal = dblDecimal.ToString
For i As Integer = 0 To (txtDecimal.Length - 1)
If txtDecimal.Substring(i, 1) = "." Then
dblAccuracy = 1 / 10 ^ (txtDecimal.Length - i)
Exit For
End If
Next
intNumerator = 0
intDenominator = 1
intNegative = 1
If dblDecimal < 0 Then
intNegative = -1
End If
dblFraction = 0
Do While Math.Abs(dblFraction - dblDecimal) > dblAccuracy
If Math.Abs(dblFraction) > Math.Abs(dblDecimal) Then
intDenominator += 1
Else
intNumerator += intNegative
End If
dblFraction = intNumerator / intDenominator
Loop
Return intNumerator.ToString
End Function
Function den(ByVal dblDecimal As Double) As String
Dim intNumerator, intDenominator, intNegative As Integer
Dim dblFraction, dblAccuracy As Double
Dim txtDecimal As String
dblAccuracy = 0.1
txtDecimal = dblDecimal.ToString
For i As Integer = 0 To (txtDecimal.Length - 1)
If txtDecimal.Substring(i, 1) = "." Then
dblAccuracy = 1 / 10 ^ (txtDecimal.Length - i)
Exit For
End If
Next
intNumerator = 0
intDenominator = 1
intNegative = 1
If dblDecimal < 0 Then
intNegative = -1
End If
dblFraction = 0
Do While Math.Abs(dblFraction - dblDecimal) > dblAccuracy
If Math.Abs(dblFraction) > Math.Abs(dblDecimal) Then
intDenominator += 1
Else
intNumerator += intNegative
End If
dblFraction = intNumerator / intDenominator
Loop
Return intDenominator.ToString
End Function