473,398 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Making a decimal to fraction converter

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 5291
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" <Ro***********@Quarters.comwrote in message
news:20**********************************@microsof t.com...
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 #2
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" <td****@community.nospamwrote in message
news:ui**************@TK2MSFTNGP02.phx.gbl...
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" <Ro***********@Quarters.comwrote in message
news:20**********************************@microsof t.com...
>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 28 '08 #3
Firstly, you can present the results from the three text boxes as a string:
text1.text & " " & text2.text & "/" & Text3.text

Secondly, any repeating decimal can be represented as the correct fraction
using code, but you haven't indicated what your problem with this procedure
is.

How is the user indicating that the decimal number is repeating
indefinately, or is the problem that you can't detect when it's a repeating
decimal? This could probably be solved with a regular expression.

Or is the problem that you don't know how to reduce a regular repeating
number to a fraction? This is relatively easy, once you know the
characteristics. For instance, if its a simple set of repeating digits,
just use:

(Repeats is the number of digits that are repeated, textbox1 has the
number 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 - 1

then simplify if possible. If it's some unique digitis followed by a
repeating portion you need to allow for that in the adjustment of the value,
and then adjust the numerator and denominator by powers of 10 until you have
eliminated the fractional part of the numerator.

"Kasha" <Ro***********@Quarters.comwrote in message
news:83**********************************@microsof t.com...
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" <td****@community.nospamwrote in message
news:ui**************@TK2MSFTNGP02.phx.gbl...
>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" <Ro***********@Quarters.comwrote in message
news:20**********************************@microso ft.com...
>>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 28 '08 #4
JDS
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" <RomulanPri...@Quarters.comwrote:
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" <tda...@community.nospamwrote in message

news:ui**************@TK2MSFTNGP02.phx.gbl...
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" <RomulanPri...@Quarters.comwrote in message
news:20**********************************@microsof t.com...
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 thisis
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!- Hide quoted text -

- Show quoted text -
Sep 29 '08 #5
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" <go****@jeremysage.comwrote in message
news:e9**********************************@m3g2000h sc.googlegroups.com...
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" <RomulanPri...@Quarters.comwrote:
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" <tda...@community.nospamwrote in message

news:ui**************@TK2MSFTNGP02.phx.gbl...
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" <RomulanPri...@Quarters.comwrote in message
news:20**********************************@microsof t.com...
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!- Hide quoted text -

- Show quoted text -
Sep 29 '08 #6
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" <jh***@yahoo.comwrote in message
news:Ov**************@TK2MSFTNGP06.phx.gbl...
Firstly, you can present the results from the three text boxes as a
string:
text1.text & " " & text2.text & "/" & Text3.text

Secondly, any repeating decimal can be represented as the correct fraction
using code, but you haven't indicated what your problem with this
procedure is.

How is the user indicating that the decimal number is repeating
indefinately, or is the problem that you can't detect when it's a
repeating decimal? This could probably be solved with a regular
expression.

Or is the problem that you don't know how to reduce a regular repeating
number to a fraction? This is relatively easy, once you know the
characteristics. For instance, if its a simple set of repeating digits,
just use:

(Repeats is the number of digits that are repeated, textbox1 has the
number 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 - 1

then simplify if possible. If it's some unique digitis followed by a
repeating portion you need to allow for that in the adjustment of the
value, and then adjust the numerator and denominator by powers of 10 until
you have eliminated the fractional part of the numerator.

"Kasha" <Ro***********@Quarters.comwrote in message
news:83**********************************@microsof t.com...
>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" <td****@community.nospamwrote in message
news:ui**************@TK2MSFTNGP02.phx.gbl...
>>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" <Ro***********@Quarters.comwrote in message
news:20**********************************@micros oft.com...
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 29 '08 #7
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" <Ro***********@Quarters.comwrote in message
news:03**********************************@microsof t.com...
>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" <jh***@yahoo.comwrote in message
news:Ov**************@TK2MSFTNGP06.phx.gbl...
>Firstly, you can present the results from the three text boxes as a
string:
text1.text & " " & text2.text & "/" & Text3.text

Secondly, any repeating decimal can be represented as the correct
fraction using code, but you haven't indicated what your problem with
this procedure is.

How is the user indicating that the decimal number is repeating
indefinately, or is the problem that you can't detect when it's a
repeating decimal? This could probably be solved with a regular
expression.

Or is the problem that you don't know how to reduce a regular repeating
number to a fraction? This is relatively easy, once you know the
characteristics. For instance, if its a simple set of repeating digits,
just use:

(Repeats is the number of digits that are repeated, textbox1 has the
number 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 - 1

then simplify if possible. If it's some unique digitis followed by a
repeating portion you need to allow for that in the adjustment of the
value, and then adjust the numerator and denominator by powers of 10
until you have eliminated the fractional part of the numerator.

"Kasha" <Ro***********@Quarters.comwrote in message
news:83**********************************@microso ft.com...
>>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" <td****@community.nospamwrote in message
news:ui**************@TK2MSFTNGP02.phx.gbl...
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" <Ro***********@Quarters.comwrote in message
news:20**********************************@micro soft.com...
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 30 '08 #8
This is the full text of a program that converts a decimal number to a
fraction, and handles repeating decimals correctly, including the case where
there is a portion of the fractional part that is non-recurring. I have
almost followed the form layout in your example, although two extra text
boxes are required to demonstrate the parameters used for the recurring
decimal case - you could simply make these internal variables instead of
text box values.

It is set up to test using manually entered numbers, for reasons that become
clear below.

For instance:
1.25 1 1 4
5.75 5 3 4
1.3 1 3 10
1.33 1 1 3
1.7272 1 8 11
0.8666 0 13 15
1.31999 1 8 25

Note that the repeating test is not foolproof - for instance it will detect
the repeating part of 1.25252 as 52, which might not be what was intended.
The cases where it fails are ambiguous, and I don't think there is any way
around the issue without some very arbitrary decisions or asking the
operator to indicate exactly what the recurring part is. This also means it
will NOT work properly with decimal numbers created from a fraction, because
the computer will usually round the last digit. So 1.66667 will not be
detected as repeating. This is the problem with your original test
procedure - you were producing decimal numbers from a fraction, but those
numbers could not be expressed in the machine as a proper repeating decimal,
so they would never be converted back to the same fraction, however good
your conversion was.

Imports System.Text.RegularExpressions
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim s() As String
Dim value As Double, numer As Double, denom As Double
Dim repeats As Double, startat As Double
TxtRepeats.Text = "0"
TxtStartAt.Text = "0"

If InStr(txtWhole.Text, ".") = 0 Then Beep() : Exit Sub
s = Split(txtWhole.Text, ".")
If s(0) = "" Then Beep() : Exit Sub
If s(1) = "" Then Beep() : Exit Sub
CheckRepeating(s(1))

value = Val(txtWhole.Text)
repeats = Val(TxtRepeats.Text) 'Number of digits per repeat sequence
startat = Val(TxtStartAt.Text) 'Position (base 1) of first digit in
first repeat seq.
startat -= 1
If startat <= 0 Then startat = repeats

If repeats = 0 Then
denom = 10 ^ Len(s(1))
numer = Val(txtWhole.Text) * denom
Else
Dim Q As String = s(1).Substring(0, startat)
Dim R As String = s(1).Substring(startat)
Dim Q1 As Integer = Val(Q & R.Substring(0, repeats)) - Val(Q)
denom = Val(StrDup(CInt(repeats), "9") & StrDup(Q.Length, "0"))
numer = Q1 + Val(s(0)) * denom
End If

txtNumer.Text = numer.ToString
txtDenom.Text = denom.ToString
Me.Refresh()
Simplify()

End Sub
Sub CheckRepeating(ByVal s As String)
Dim MyRegex As Regex = New Regex("(\d+)\1{1,}\z")
If Not MyRegex.IsMatch(s) Then Exit Sub

Dim ms As MatchCollection = MyRegex.Matches(s)
Dim m As String = ms.Item(ms.Count - 1).Groups("1").Value

TxtRepeats.Text = m.Length
TxtStartAt.Text = InStr(s, m)

End Sub

Private Sub Simplify()
Dim n As Long = Val(txtNumer.Text)
Dim d As Long = Val(txtDenom.Text)
Dim i As Long

For i = n To 1 Step -1
If (n / i = Int(n / i)) And (d / i = Int(d / i)) Then Exit For
Next
n = n / i
d = d / i
i = 0

If d 0 Then
While n d
i = i + 1 : n = n - d
End While
End If
TxtResultW.Text = i.ToString
TxtResultN.Text = n.ToString
TxtResultD.Text = d.ToString

End Sub
End Class
"Kasha" <Ro***********@Quarters.comwrote in message
news:03**********************************@microsof t.com...
>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" <jh***@yahoo.comwrote in message
news:Ov**************@TK2MSFTNGP06.phx.gbl...
>Firstly, you can present the results from the three text boxes as a
string:
text1.text & " " & text2.text & "/" & Text3.text

Secondly, any repeating decimal can be represented as the correct
fraction using code, but you haven't indicated what your problem with
this procedure is.

How is the user indicating that the decimal number is repeating
indefinately, or is the problem that you can't detect when it's a
repeating decimal? This could probably be solved with a regular
expression.

Or is the problem that you don't know how to reduce a regular repeating
number to a fraction? This is relatively easy, once you know the
characteristics. For instance, if its a simple set of repeating digits,
just use:

(Repeats is the number of digits that are repeated, textbox1 has the
number 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 - 1

then simplify if possible. If it's some unique digitis followed by a
repeating portion you need to allow for that in the adjustment of the
value, and then adjust the numerator and denominator by powers of 10
until you have eliminated the fractional part of the numerator.

"Kasha" <Ro***********@Quarters.comwrote in message
news:83**********************************@microso ft.com...
>>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" <td****@community.nospamwrote in message
news:ui**************@TK2MSFTNGP02.phx.gbl...
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" <Ro***********@Quarters.comwrote in message
news:20**********************************@micro soft.com...
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 30 '08 #9
Thanks! Works great =]

-Jason

"James Hahn" <jh***@yahoo.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
This is the full text of a program that converts a decimal number to a
fraction, and handles repeating decimals correctly, including the case
where there is a portion of the fractional part that is non-recurring. I
have almost followed the form layout in your example, although two extra
text boxes are required to demonstrate the parameters used for the
recurring decimal case - you could simply make these internal variables
instead of text box values.

It is set up to test using manually entered numbers, for reasons that
become clear below.

For instance:
1.25 1 1 4
5.75 5 3 4
1.3 1 3 10
1.33 1 1 3
1.7272 1 8 11
0.8666 0 13 15
1.31999 1 8 25

Note that the repeating test is not foolproof - for instance it will
detect the repeating part of 1.25252 as 52, which might not be what was
intended. The cases where it fails are ambiguous, and I don't think there
is any way around the issue without some very arbitrary decisions or
asking the operator to indicate exactly what the recurring part is. This
also means it will NOT work properly with decimal numbers created from a
fraction, because the computer will usually round the last digit. So
1.66667 will not be detected as repeating. This is the problem with your
original test procedure - you were producing decimal numbers from a
fraction, but those numbers could not be expressed in the machine as a
proper repeating decimal, so they would never be converted back to the
same fraction, however good your conversion was.

Imports System.Text.RegularExpressions
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim s() As String
Dim value As Double, numer As Double, denom As Double
Dim repeats As Double, startat As Double
TxtRepeats.Text = "0"
TxtStartAt.Text = "0"

If InStr(txtWhole.Text, ".") = 0 Then Beep() : Exit Sub
s = Split(txtWhole.Text, ".")
If s(0) = "" Then Beep() : Exit Sub
If s(1) = "" Then Beep() : Exit Sub
CheckRepeating(s(1))

value = Val(txtWhole.Text)
repeats = Val(TxtRepeats.Text) 'Number of digits per repeat
sequence
startat = Val(TxtStartAt.Text) 'Position (base 1) of first digit in
first repeat seq.
startat -= 1
If startat <= 0 Then startat = repeats

If repeats = 0 Then
denom = 10 ^ Len(s(1))
numer = Val(txtWhole.Text) * denom
Else
Dim Q As String = s(1).Substring(0, startat)
Dim R As String = s(1).Substring(startat)
Dim Q1 As Integer = Val(Q & R.Substring(0, repeats)) - Val(Q)
denom = Val(StrDup(CInt(repeats), "9") & StrDup(Q.Length, "0"))
numer = Q1 + Val(s(0)) * denom
End If

txtNumer.Text = numer.ToString
txtDenom.Text = denom.ToString
Me.Refresh()
Simplify()

End Sub
Sub CheckRepeating(ByVal s As String)
Dim MyRegex As Regex = New Regex("(\d+)\1{1,}\z")
If Not MyRegex.IsMatch(s) Then Exit Sub

Dim ms As MatchCollection = MyRegex.Matches(s)
Dim m As String = ms.Item(ms.Count - 1).Groups("1").Value

TxtRepeats.Text = m.Length
TxtStartAt.Text = InStr(s, m)

End Sub

Private Sub Simplify()
Dim n As Long = Val(txtNumer.Text)
Dim d As Long = Val(txtDenom.Text)
Dim i As Long

For i = n To 1 Step -1
If (n / i = Int(n / i)) And (d / i = Int(d / i)) Then Exit For
Next
n = n / i
d = d / i
i = 0

If d 0 Then
While n d
i = i + 1 : n = n - d
End While
End If
TxtResultW.Text = i.ToString
TxtResultN.Text = n.ToString
TxtResultD.Text = d.ToString

End Sub
End Class
"Kasha" <Ro***********@Quarters.comwrote in message
news:03**********************************@microsof t.com...
>>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" <jh***@yahoo.comwrote in message
news:Ov**************@TK2MSFTNGP06.phx.gbl...
>>Firstly, you can present the results from the three text boxes as a
string:
text1.text & " " & text2.text & "/" & Text3.text

Secondly, any repeating decimal can be represented as the correct
fraction using code, but you haven't indicated what your problem with
this procedure is.

How is the user indicating that the decimal number is repeating
indefinately, or is the problem that you can't detect when it's a
repeating decimal? This could probably be solved with a regular
expression.

Or is the problem that you don't know how to reduce a regular repeating
number to a fraction? This is relatively easy, once you know the
characteristics. For instance, if its a simple set of repeating digits,
just use:

(Repeats is the number of digits that are repeated, textbox1 has the
number 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 - 1

then simplify if possible. If it's some unique digitis followed by a
repeating portion you need to allow for that in the adjustment of the
value, and then adjust the numerator and denominator by powers of 10
until you have eliminated the fractional part of the numerator.

"Kasha" <Ro***********@Quarters.comwrote in message
news:83**********************************@micros oft.com...
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" <td****@community.nospamwrote in message
news:ui**************@TK2MSFTNGP02.phx.gbl...
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" <Ro***********@Quarters.comwrote in message
news:20**********************************@micr osoft.com...
>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!
>
>

Oct 7 '08 #10
Thanks for letting us know that the information was useful.

"Kasha" <Ro***********@Quarters.comwrote in message
news:2F**********************************@microsof t.com...
Thanks! Works great =]

-Jason
Oct 8 '08 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
by: Batista, Facundo | last post by:
Here I send it. Suggestions and all kinds of recomendations are more than welcomed. If it all goes ok, it'll be a PEP when I finish writing/modifying the code. Thank you. .. Facundo
5
by: Michael A. Covington | last post by:
Microsoft's documentation is a bit unclear, but is Decimal in fact a radix-100 (base-100) arithmetic data type (in which each byte ranges only from 0 to 99)? It should be straightforward to dump...
9
by: John Cho | last post by:
// CHO, JOHN #include<iostream> class fracpri{ int whole; int numer; int denom;
13
by: Steve | last post by:
I am having trouble finding the answer to this question, I'm thinking the solution must be blindingly obvious, so therefore of course I cannot see it. I wish to take a fraction in DEC say .4 and...
7
by: Oenone | last post by:
Can anyone explain why the following happens? \\\ Dim d1 As Decimal = CDec("100") Dim d2 As Decimal = CDec("100.00") MsgBox(d1.ToString) 'displays "100" MsgBox(d2.ToString) 'displays...
0
by: guindy | last post by:
Hello friends , I am a New bee to VB . I Need a solution for a little thing Consider a situation where the result is of a floating point value. I need to print the value separately in two...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
0
by: Stefan Sonnenberg-Carstens | last post by:
Hi, I was wondering if there are any tricks around to do some sort of changing types, float in particular. I do many operations like summing etc on lots of floats and always have to do some extra...
4
by: Astley Le Jasper | last post by:
I've been getting errors recently when using pysqlite. I've declared the table columns as real numbers to 2 decimal places (I'm dealing with money), but when doing division on two numbers that...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.