458,146 Members | 1,820 Online Need help? Post your question and get tips & solutions from a community of 458,146 IT Pros & Developers. It's quick & easy.

# Making a decimal to fraction converter

 P: n/a I'm making a program that will convert decimal inputs (in this case, in inches) and output a fractional answer. At the moment, I'm only able to output the fractional answer in three parts: A whole number text box, a numerator text box, and a denominator text box. I realize that this is probably the closest I'll get to having fractions displayed in VB, so that's no big deal. I'm able to simplify most numbers with some code I've written, but I'm unable to simplify anything that repeats infinitely (i.e.. 1/3). Is there any way to be able to simplify this? I can give you the code I've already written if that would help. Thanks! Sep 27 '08 #1
10 Replies

 P: n/a Jason, if you can have your users specify the finest graduation to output (say, 64ths, 8ths, quarters, etc.), or if you assume a certain finest graduation, you can convert a repeating fraction and then round to the finest graduation that the number converts to, and then reduce it if possible. So for instance, your 1/3 is 0.33333... If your finest graduation is 128ths, multiply the 0.33333... by 128, getting 42.66666... and round to 43, giving youi 43 / 128ths. Similarly, 1/9 is 0.11111... For 128ths, multiply 0.11111... by 128, getting 14.22222... Round to 14/128ths and reduce to 7/64ths. Tom Dacon Dacon Software Consulting "Jason"

 P: n/a The goal of the program is to convert decimals to fractions with only the input of a decimal number, and thus far, I've only been able to simplify nonrepeating decimal numbers. If I were to have the user specify the finest graduation, I'd need a drastically different code, and I'd be going beyond the goal of the program =\ Is my goal hopeless without specifying the finest graduation then? -Jason "Tom Dacon" I'm making a program that will convert decimal inputs (in this case, ininches) and output a fractional answer. At the moment, I'm only able tooutput the fractional answer in three parts: A whole number text box, anumerator text box, and a denominator text box. I realize that this isprobably the closest I'll get to having fractions displayed in VB, sothat's no big deal. I'm able to simplify most numbers with some codeI've written, but I'm unable to simplify anything that repeats infinitely(i.e.. 1/3). Is there any way to be able to simplify this?I can give you the code I've already written if that would help.Thanks! Sep 28 '08 #3

 P: n/a I can't see how you could do it without somehow specify the acceptable accuracy. Otherwise, how can any code determine whether 0.33 should return 33/100 or 1/3 (or whether 0.33333 should return 33333/100000 or 1/3)? Whether you as the programmer specify the accuracy or whether the user specifies the accuracy I guess depends on the application. Another option may be for the user to specify that the input is recurring but again, this would depend on the application and whether it is an acceptable solution. HTH On Sep 28, 8:30*pm, "Kasha"

 P: n/a The common standard for indicating a repeating decimal is a trailing period. In that case, "0.33." would convert to 1/3, using the code I posted. "JDS"

 P: n/a I AM using three text boxes =] , one for the whole number, one for the numerator, and one for the denomenator. Dim denomtest As Integer Dim numertest As Integer Dim dota As Boolean Dim dotb As Boolean Dim time As Integer = 1 Dim num As Integer = 2 Dim numertesthold As Integer Dim denomtesthold As Integer Dim numertesta As Integer Dim denomtesta As Integer Dim frac As Double Dim i As Integer Private Sub convert() IF int(numer.Text) = 66 THEN numertest = 1 denomtest = 6 Goto chocolate cake Endif time = 1 denomtesthold = denom.Text numertesthold = numer.Text top: Do Until time = 100 For num = 2 To 51 If num = 51 Then GoTo chocolate numertesta = numertesthold denomtesta = denomtesthold numertest = numertesta / num denomtest = denomtesta / num For i = 2 To Len(numertest) If dota = True Or dota = False Then GoTo Here If Mid(numertest, i, 1) = "." Then dota = True If i = Len(numertest) Then dota = False Next Here: For i = 2 To Len(denomtest) If dotb = True Or dotb = False Then GoTo There If Mid(denomtest, i, 1) = "." Then dotb = True If i = Len(denomtest) Then dotb = False Next There: If dota = False And dotb = False And denomtest <0 And numertest <0 And numertest / denomtest = frac Then i = 2 time = time + 1 denomtesthold = denomtest numertesthold = numertest GoTo top End If Next Loop chocolate: End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Whole.Text = Int(Inch.Text) numer.Text = (Inch.Text - whole.Text) * 10000000 denom.Text = 10000000 frac = numer.Text / denom.Text Call convert() Whole.Text = Int(Inch.Text) Numer.Text = numertesthold Denom.Text = denomtesthold End Sub End Class That's how far I currently am. If I were to use your code, where would I place it? "James Hahn" The goal of the program is to convert decimals to fractions with only theinput of a decimal number, and thus far, I've only been able to simplifynonrepeating decimal numbers. If I were to have the user specify thefinest graduation, I'd need a drastically different code, and I'd begoing beyond the goal of the program =\Is my goal hopeless without specifying the finest graduation then?-Jason"Tom Dacon" >Jason, if you can have your users specify the finest graduation tooutput (say, 64ths, 8ths, quarters, etc.), or if you assume a certainfinest graduation, you can convert a repeating fraction and then roundto the finest graduation that the number converts to, and then reduce itif possible.So for instance, your 1/3 is 0.33333... If your finest graduation is128ths, multiply the 0.33333... by 128, getting 42.66666... and round to43, giving youi 43 / 128ths.Similarly, 1/9 is 0.11111... For 128ths, multiply 0.11111... by 128,getting 14.22222... Round to 14/128ths and reduce to 7/64ths. Tom DaconDacon Software Consulting"Jason"

 P: n/a Whoops, here's the real code Dim denomtest As Integer Dim numertest As Integer Dim dota As Boolean Dim dotb As Boolean Dim time As Integer = 1 Dim num As Integer = 2 Dim numertesthold As Integer Dim denomtesthold As Integer Dim numertesta As Integer Dim denomtesta As Integer Dim frac As Double Dim i As Integer Private Sub convert() time = 1 denomtesthold = denom.Text numertesthold = numer.Text top: Do Until time = 100 For num = 2 To 51 If num = 51 Then GoTo chocolate numertesta = numertesthold denomtesta = denomtesthold numertest = numertesta / num denomtest = denomtesta / num For i = 2 To Len(numertest) If dota = True Or dota = False Then GoTo Here If Mid(numertest, i, 1) = "." Then dota = True If i = Len(numertest) Then dota = False Next Here: For i = 2 To Len(denomtest) If dotb = True Or dotb = False Then GoTo There If Mid(denomtest, i, 1) = "." Then dotb = True If i = Len(denomtest) Then dotb = False Next There: If dota = False And dotb = False And denomtest <0 And numertest <0 And numertest / denomtest = frac Then i = 2 time = time + 1 denomtesthold = denomtest numertesthold = numertest GoTo top End If Next Loop chocolate: End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Whole.Text = Int(Inch.Text) numer.Text = (Inch.Text - whole.Text) * 10000000 denom.Text = 10000000 frac = numer.Text / denom.Text Call convert() Whole.Text = Int(Inch.Text) Numer.Text = numertesthold Denom.Text = denomtesthold End Sub End Class "Kasha" I AM using three text boxes =] , one for the whole number, one for thenumerator, and one for the denomenator. Dim denomtest As Integer Dim numertest As Integer Dim dota As Boolean Dim dotb As Boolean Dim time As Integer = 1 Dim num As Integer = 2 Dim numertesthold As Integer Dim denomtesthold As Integer Dim numertesta As Integer Dim denomtesta As Integer Dim frac As Double Dim i As Integer Private Sub convert() IF int(numer.Text) = 66 THEN numertest = 1 denomtest = 6 Goto chocolate cake Endif time = 1 denomtesthold = denom.Text numertesthold = numer.Text top: Do Until time = 100 For num = 2 To 51 If num = 51 Then GoTo chocolate numertesta = numertesthold denomtesta = denomtesthold numertest = numertesta / num denomtest = denomtesta / num For i = 2 To Len(numertest) If dota = True Or dota = False Then GoTo Here If Mid(numertest, i, 1) = "." Then dota = True If i = Len(numertest) Then dota = False Next Here: For i = 2 To Len(denomtest) If dotb = True Or dotb = False Then GoTo There If Mid(denomtest, i, 1) = "." Then dotb = True If i = Len(denomtest) Then dotb = False Next There: If dota = False And dotb = False And denomtest <0 And numertest <0 And numertest / denomtest = frac Then i = 2 time = time + 1 denomtesthold = denomtest numertesthold = numertest GoTo top End If Next Loop chocolate: End Sub Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Whole.Text = Int(Inch.Text) numer.Text = (Inch.Text - whole.Text) * 10000000 denom.Text = 10000000 frac = numer.Text / denom.Text Call convert() Whole.Text = Int(Inch.Text) Numer.Text = numertesthold Denom.Text = denomtesthold End Sub End Class That's how far I currently am. If I were to use your code, where would I place it? "James Hahn" Firstly, you can present the results from the three text boxes as astring: text1.text & " " & text2.text & "/" & Text3.textSecondly, any repeating decimal can be represented as the correctfraction using code, but you haven't indicated what your problem withthis procedure is.How is the user indicating that the decimal number is repeatingindefinately, or is the problem that you can't detect when it's arepeating decimal? This could probably be solved with a regularexpression.Or is the problem that you don't know how to reduce a regular repeatingnumber to a fraction? This is relatively easy, once you know thecharacteristics. For instance, if its a simple set of repeating digits,just use: (Repeats is the number of digits that are repeated, textbox1 has thenumber to convert) s = Split(TextBox1.Text, ".") s(0) = s(0) & s(1).Substring(0, repeats) numerator = Val(s(0) & "." & s(1)) power = 10 ^ repeats numerator = numerator - value denominator = power - 1then simplify if possible. If it's some unique digitis followed by arepeating portion you need to allow for that in the adjustment of thevalue, and then adjust the numerator and denominator by powers of 10until you have eliminated the fractional part of the numerator."Kasha" >The goal of the program is to convert decimals to fractions with onlythe input of a decimal number, and thus far, I've only been able tosimplify nonrepeating decimal numbers. If I were to have the userspecify the finest graduation, I'd need a drastically different code,and I'd be going beyond the goal of the program =\Is my goal hopeless without specifying the finest graduation then?-Jason"Tom Dacon" I can give you the code I've already written if that would help.>Thanks! Sep 30 '08 #8 