A better way to go is to use the Euclidean Algorithm to find the gcd.
After finding the gcd, the rest is simple.
Code:''' <summary> ''' Return a fraction string from a double. ''' </summary> ''' <param name="d">The double to convert.</param> ''' <returns>The converted string.</returns> ''' <remarks>Code written by Troy Lundin on May 3, 2007</remarks> Function GetFraction(ByVal d As Double) As String ' Get the initial denominator: 1 * (10 ^ decimal portion length) Dim Denom As Int32 = CInt(1 * (10 ^ tb1.Text.Split("."c)(1).Length)) ' Get the initial numerator: integer portion of the number Dim Numer As Int32 = CInt(tb1.Text.Split("."c)(1)) ' Use the Euclidean algorithm to find the gcd Dim a As Int32 = Numer Dim b As Int32 = Denom Dim t As Int32 = 0 ' t is a value holder ' Euclidean algorithm While b <> 0 t = b b = a Mod b a = t End While ' Return our answer Return CInt(d) & " " & (Numer / a) & "/" & (Denom / a) End Function




Reply With Quote