473,402 Members | 2,061 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,402 software developers and data experts.

Converting Decimal to Fraction

Hi guys, I'm new to VB and I am having trouble reverse engineering some code i got from VB.net

I Want to add 2 fractions together and give an answer in a textbox when the solve button is pressed.

I am getting the whole #'s but not fractions when I click the button.

Here's how the form is set up.

4 boxes are on the form the user imputs the numerator and denominator of 2 fractions to be added.

Clicks on the solve button then the answer is to be displayed into a 5th box.

I will show both my code and the original.

I am using VB.net Framework 2003 on XP system

My code
Expand|Select|Wrap|Line Numbers
  1. ' Converts the two fractions entered into decimals and adds them 
  2.     ' Then converts the Decimal answer  back into a fraction
  3.  
  4.  
  5.  
  6.     Private Sub AddFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFract.Click
  7.  
  8.  
  9.         Dim AddFractSolve As Long
  10.         Dim LargestDenominator As Long
  11.         Dim GCD As Long
  12.         Dim TopNumber As Long
  13.         Dim Remainder As Long
  14.         Dim WholeNumber As Long
  15.         Dim Numerator As Long
  16.         Dim Denominator As Long
  17.         Dim DecimalNumber As Long
  18.  
  19.         AddFractSolve = (Val(AddNum1.Text) / Val(AddDen1.Text)) + (Val(AddNum2.Text) / Val(AddDen2.Text))
  20.  
  21.         If IsNumeric(AddFractSolve) Then
  22.  
  23.             AddFractSolve = CDbl(AddFractSolve)
  24.             WholeNumber = Fix(AddFractSolve)
  25.             Denominator = LargestDenominator
  26.             Numerator = Format(Denominator * _
  27.              Math.Abs(DecimalNumber - WholeNumber), "0")
  28.  
  29.             If Numerator Then
  30.  
  31.                 GCD = LargestDenominator
  32.                 TopNumber = Numerator
  33.  
  34.                 Do
  35.  
  36.                     Remainder = (GCD Mod TopNumber)
  37.                     GCD = TopNumber
  38.                     TopNumber = Remainder
  39.  
  40.                 Loop Until Remainder = 0
  41.  
  42.                 Numerator = Numerator \ GCD
  43.                 Denominator = Denominator \ GCD
  44.                 AddFractAns1.Text = "" & (WholeNumber) & _
  45.                                         "and" & _
  46.                                        (Numerator) & "/" & _
  47.                                        (Denominator)
  48.  
  49.             Else
  50.  
  51.                 AddFractAns1.Text = (WholeNumber)
  52.  
  53.             End If
  54.  
  55.         Else
  56.  
  57.  
  58.         End If
  59.  
  60.  
  61.  
  62.     End Sub
  63.  
original code
Expand|Select|Wrap|Line Numbers
  1. Function MakeFraction(ByVal DecimalNumber As Variant, _
  2.                       Optional ByVal LargestDenominator As Long = 64, _
  3.                       Optional bShowDash As Boolean = False) As String
  4.  
  5.    Dim GCD As Long
  6.    Dim TopNumber As Long
  7.    Dim Remainder As Long
  8.    Dim WholeNumber As Long
  9.    Dim Numerator As Long
  10.    Dim Denominator As Long
  11.  
  12.    If IsNumeric(DecimalNumber) Then
  13.  
  14.       DecimalNumber = CDbl(DecimalNumber)
  15.       WholeNumber = Fix(DecimalNumber)
  16.       Denominator = LargestDenominator
  17.       Numerator = Format(Denominator * _
  18.                            Abs(DecimalNumber - WholeNumber), "0")
  19.  
  20.       If Numerator Then
  21.  
  22.          GCD = LargestDenominator
  23.          TopNumber = Numerator
  24.  
  25.          Do
  26.  
  27.             Remainder = (GCD Mod TopNumber)
  28.             GCD = TopNumber
  29.             TopNumber = Remainder
  30.  
  31.          Loop Until Remainder = 0
  32.  
  33.          Numerator = Numerator \ GCD
  34.          Denominator = Denominator \ GCD
  35.  
  36.          MakeFraction = CStr(WholeNumber) & _
  37.                         IIf(bShowDash, "-", "  ") & _
  38.                         CStr(Numerator) & "/" & _
  39.                         CStr(Denominator)
  40.       Else
  41.  
  42.         MakeFraction = CStr(WholeNumber)
  43.  
  44.       End If
  45.  
  46.    Else
  47.  
  48.      'Input wasn't a number, handle error here
  49.  
  50.    End If
  51.  
  52. End Function
  53.  
Any help would be greatly appreciated

Thank you'
Semajthewise
Nov 14 '07 #1
3 1512
Shashi Sadasivan
1,435 Expert 1GB
Hi,
have you tried had a look at this
Nov 14 '07 #2
Hi,
have you tried had a look at this
I looked at it thank you.
I got the code I was writing to work but there was some rounding problems with it
So I went back to the drawing board and wrote this

Expand|Select|Wrap|Line Numbers
  1.   ' setting up my variables
  2.         Dim num1add As Double = AddNum1.Text
  3.         Dim num2add As Double = AddNum2.Text
  4.         Dim den1add As Double = AddDen1.Text
  5.         Dim den2add As Double = AddDen2.Text
  6.         Dim numansadd As Double
  7.         Dim denansadd As Double
  8.         Dim WholeNumberadd As Double
  9.  
  10.         ' if denominators  are different in text boxes then run this
  11.         If den1add <> den2add Then
  12.  
  13.             'creates the numerator of the answer
  14.             numansadd = ((num1add * den2add) + (num2add * den1add))
  15.             'creates the denominator of the answer
  16.             denansadd = (den1add * den2add)
  17.  
  18.             ' if denominators match run this
  19.         Else
  20.             ' adds the 2 numerators to create an answer
  21.             numansadd = (num1add + num2add)
  22.             ' keeps the matching denominators
  23.             denansadd = (den1add)
  24.  
  25.         End If
  26.         ' after adding if the numerator is higher that the denominatoer run this
  27.         If numansadd >= denansadd Then
  28.  
  29.             ' setting variables
  30.             Dim LoopCountadd As Integer
  31.             Dim TimesLoopedadd As Integer
  32.             TimesLoopedadd = 0
  33.             LoopCountadd = 0
  34.  
  35.             ' a loop to subtract the denominator from the numerator until the numerator_
  36.             ' is smaller than the denominator
  37.  
  38.             Do While numansadd >= denansadd
  39.                 ' subtracts the numerator from the denominator
  40.                 TimesLoopedadd = (denansadd - numansadd)
  41.                 ' counts up 1 for every time the numerator is subtracted from the denominator
  42.                 LoopCountadd += 1
  43.                 ' makes WholeNumber = the total of times the loop occurred
  44.                 WholeNumberadd = LoopCountadd
  45.                 ' makes the remainder = the new numerator answer
  46.                 numansadd = (numansadd - denansadd)
  47.  
  48.                 ' goes back to see if the numerator is higher than the denominator
  49.             Loop
  50.  
  51.         End If
  52.  
  53.         ' if the new numerator = 0 only the whole number is displayed
  54.         If numansadd = 0 Then
  55.  
  56.             AddFractAns1.Text = "" & (WholeNumberadd)
  57.  
  58.             ' otherwise the whole number and the remaining fraction are displayed
  59.         Else
  60.  
  61.             AddFractAns1.Text = "" & (WholeNumberadd) & " and " _
  62.             & (numansadd) & "/" & (denansadd)
  63.  
  64.         End If
  65.  
  66.  
  67.  
  68.  
This works perfectly but now I need to write the code to reduce the final answer. :) wish me luck.

You will notice that it is very heavily notated I do this since I am still fairly new to VB and it helps later if I need to re-engineer code for something later.
Nov 20 '07 #3
Hi,
have you tried had a look at this
I finished it finally thanks again Shashi. Here's what I finally came up with and it works perfectly. tho probably over engeneered :)

Function.
Expand|Select|Wrap|Line Numbers
  1. ' Returns the greatest common divisor using Euclid's algorithm
  2.     Private Function GCD(ByVal x As Integer, ByVal y As Integer) As Integer
  3.         Dim temp As Integer
  4.  
  5.         x = Math.Abs(x)
  6.         y = Math.Abs(y)
  7.  
  8.         Do While (y <> 0)
  9.             temp = x Mod y
  10.             x = y
  11.             y = temp
  12.         Loop
  13.  
  14.         Return x
  15.     End Function
Addition
Expand|Select|Wrap|Line Numbers
  1.  ' Setting up my variables
  2.         Dim num1add As Integer = AddNum1.Text
  3.         Dim num2add As Integer = AddNum2.Text
  4.         Dim den1add As Integer = AddDen1.Text
  5.         Dim den2add As Integer = AddDen2.Text
  6.         Dim numansadd As Integer
  7.         Dim denansadd As Integer
  8.         Dim WholeNumberAdd As Integer
  9.         Dim ReduceFraction As Integer
  10.  
  11.         ' If denominators  are different in text boxes then run this
  12.         If den1add <> den2add Then
  13.  
  14.             'Creates the numerator of the answer
  15.             numansadd = ((num1add * den2add) + (num2add * den1add))
  16.             'Creates the denominator of the answer
  17.             denansadd = (den1add * den2add)
  18.  
  19.             ' If denominators match run this
  20.         Else
  21.             ' Adds the 2 numerators to create an answer
  22.             numansadd = (num1add + num2add)
  23.             ' Keeps the matching denominators
  24.             denansadd = (den1add)
  25.  
  26.         End If
  27.         ' After adding if the numerator is higher that the denominatoer run this
  28.         If numansadd >= denansadd Then
  29.  
  30.             ' Setting variables
  31.             Dim LoopCountadd As Integer
  32.             Dim TimesLoopedadd As Integer
  33.             TimesLoopedadd = 0
  34.             LoopCountadd = 0
  35.  
  36.             ' A loop to subtract the denominator from the numerator until the numerator_
  37.             ' is smaller than the denominator
  38.  
  39.             Do While numansadd >= denansadd
  40.                 ' Subtracts the numerator from the denominator
  41.                 TimesLoopedadd = (denansadd - numansadd)
  42.                 ' Counts up 1 for every time the numerator is subtracted from the denominator
  43.                 LoopCountadd += 1
  44.                 ' Makes WholeNumber = the total of times the loop occurred
  45.                 WholeNumberAdd = LoopCountadd
  46.                 ' Makes the remainder = the new numerator answer
  47.                 numansadd = (numansadd - denansadd)
  48.  
  49.                 ' Goes back to see if the numerator is higher than the denominator
  50.             Loop
  51.  
  52.         End If
  53.  
  54.         ' Using the function GCD to get the greatest common divisor _
  55.         ' With numansadd and denansadd as the variables
  56.         ReduceFraction = GCD(numansadd, denansadd)
  57.         ' Divides the numerator by the greatest common divisor
  58.         numansadd = (numansadd / ReduceFraction)
  59.         ' Divides the denominator by the greatest common divisor
  60.         denansadd = (denansadd / ReduceFraction)
  61.  
  62.         ' If the new numerator = 0 only the whole number is displayed
  63.         If numansadd = 0 Then
  64.  
  65.             AddFractAns1.Text = "" & (WholeNumberAdd)
  66.  
  67.             ' If there is no whole number then display the fraction only
  68.         ElseIf WholeNumberAdd = 0 Then
  69.             AddFractAns1.Text = "" & (numansadd) & "/" & (denansadd)
  70.  
  71.             ' Otherwise the whole number and the remaining fraction are displayed
  72.         Else
  73.  
  74.             AddFractAns1.Text = "" & (WholeNumberAdd) & " and " _
  75.             & (numansadd) & "/" & (denansadd)
  76.  
  77.         End If
  78.  
subtraction
Expand|Select|Wrap|Line Numbers
  1. ' Setting up my variables
  2.         Dim num1sub As Integer = SubNum1.Text
  3.         Dim num2sub As Integer = SubNum2.Text
  4.         Dim den1sub As Integer = SubDen1.Text
  5.         Dim den2sub As Integer = SubDen2.Text
  6.         Dim numanssub As Integer
  7.         Dim denanssub As Integer
  8.         Dim WholeNumbersub As Integer
  9.         Dim ReduceFraction As Integer
  10.  
  11.         ' If denominators  are different in text boxes then run this
  12.         If den1sub <> den2sub Then
  13.  
  14.             'Creates the numerator of the answer
  15.             numanssub = ((num1sub * den2sub) - (num2sub * den1sub))
  16.             'Creates the denominator of the answer
  17.             denanssub = (den1sub * den2sub)
  18.  
  19.             ' If denominators match run this
  20.         Else
  21.             ' Adds the 2 numerators to create an answer
  22.             numanssub = (num1sub - num2sub)
  23.             ' Keeps the matching denominators
  24.             denanssub = (den1sub)
  25.  
  26.         End If
  27.         ' After the math. if the numerator is higher that the denominatoer run this
  28.         If Math.Abs(numanssub) >= Math.Abs(denanssub) Then
  29.  
  30.             ' Setting variables
  31.             Dim LoopCountadd As Integer
  32.             Dim TimesLoopedadd As Integer
  33.             TimesLoopedadd = 0
  34.             LoopCountadd = 0
  35.  
  36.             ' A loop to subtract the denominator from the numerator until the numerator_
  37.             ' is smaller than the denominator
  38.  
  39.             Do While Math.Abs(numanssub) >= Math.Abs(denanssub)
  40.                 ' Subtracts the numerator from the denominator
  41.                 TimesLoopedadd = (Math.Abs(denanssub) - Math.Abs(numanssub))
  42.                 ' Counts up 1 for every time the numerator is subtracted from the denominator
  43.                 LoopCountadd += -1
  44.                 ' Makes WholeNumber = the total of times the loop occurred
  45.                 WholeNumbersub = LoopCountadd
  46.                 ' Makes the remainder = the new numerator answer
  47.                 numanssub = (Math.Abs(numanssub) - Math.Abs(denanssub))
  48.  
  49.                 ' Goes back to see if the numerator is higher than the denominator
  50.             Loop
  51.  
  52.         End If
  53.  
  54.         ' Using the function GCD to get the greatest common divisor _
  55.         ' with numansadd and denansadd as the variables
  56.         ReduceFraction = GCD(numanssub, denanssub)
  57.         ' Divides the numerator by the greatest common divisor
  58.         numanssub = (numanssub / ReduceFraction)
  59.         ' Divides the denominator by the greatest common divisor
  60.         denanssub = (denanssub / ReduceFraction)
  61.  
  62.         ' If the new numerator = 0 only the whole number is displayed
  63.         If numanssub = 0 Then
  64.  
  65.             SubFractAns1.Text = "" & (WholeNumbersub)
  66.  
  67.             ' Of there is no whole number then display the fraction only
  68.         ElseIf WholeNumbersub = 0 Then
  69.             SubFractAns1.Text = "" & (numanssub) & "/" & (denanssub)
  70.  
  71.             ' Otherwise the whole number and the remaining fraction are displayed
  72.         Else
  73.  
  74.             SubFractAns1.Text = "" & (WholeNumbersub) & " and " _
  75.             & (numanssub) & "/" & (denanssub)
  76.  
  77.         End If
  78.  
Multiplication
Expand|Select|Wrap|Line Numbers
  1. ' Setting my variables
  2.         Dim num1multi As Integer = MultiNum1.Text
  3.         Dim num2multi As Integer = MultiNum2.Text
  4.         Dim den1multi As Integer = MultiDen1.Text
  5.         Dim den2multi As Integer = MultiDen2.Text
  6.         Dim numansmulti As Integer = (num1multi * num2multi)
  7.         Dim denansmulti As Integer = (den1multi * den2multi)
  8.         Dim ReduceFractionmulti = GCD(numansmulti, denansmulti)
  9.         Dim WholeNumberMulti As Integer
  10.  
  11.         ' This Reduces the fraction down to the least common denominator using the function GCD
  12.         numansmulti = (numansmulti / ReduceFractionmulti)
  13.         denansmulti = (denansmulti / ReduceFractionmulti)
  14.  
  15.         ' After the math. If the numerator is higher that the denominatoer run this
  16.         If Math.Abs(numansmulti) >= Math.Abs(denansmulti) Then
  17.  
  18.             ' Setting variables
  19.             Dim LoopCountadd As Integer
  20.             Dim TimesLoopedadd As Integer
  21.             TimesLoopedadd = 0
  22.             LoopCountadd = 0
  23.  
  24.             ' A loop to subtract the denominator from the numerator until the numerator_
  25.             ' is smaller than the denominator
  26.             Do While Math.Abs(numansmulti) >= Math.Abs(denansmulti)
  27.                 ' Subtracts the numerator from the denominator
  28.                 TimesLoopedadd = (Math.Abs(denansmulti) - Math.Abs(numansmulti))
  29.                 ' Counts up 1 for every time the numerator is subtracted from the denominator
  30.                 LoopCountadd += 1
  31.                 ' Makes WholeNumber = the total of times the loop occurred
  32.                 WholeNumberMulti = LoopCountadd
  33.                 ' Makes the remainder = the new numerator answer
  34.                 numansmulti = (Math.Abs(numansmulti) - Math.Abs(denansmulti))
  35.  
  36.                 ' Goes back to see if the numerator is higher than the denominator
  37.             Loop
  38.  
  39.         End If
  40.         ' If there is no fraction display only the whole number
  41.         If numansmulti = 0 Then
  42.  
  43.             MultiFractAns1.Text = "" & (WholeNumberMulti)
  44.  
  45.             ' If there is no whole number then display the fraction only
  46.         ElseIf WholeNumberMulti = 0 Then
  47.  
  48.             MultiFractAns1.Text = "" & (numansmulti) & "/" & (denansmulti)
  49.  
  50.             ' Otherwise the whole number and the remaining fraction are displayed
  51.         Else
  52.  
  53.             MultiFractAns1.Text = "" & (WholeNumberMulti) & " and " _
  54.             & (numansmulti) & "/" & (denansmulti)
  55.  
  56.         End If
  57.  
Division
Expand|Select|Wrap|Line Numbers
  1.  ' Setting my variables
  2.         Dim num1div As Integer = DivNum1.Text
  3.         Dim num2div As Integer = DivNum2.Text
  4.         Dim den1div As Integer = DivDen1.Text
  5.         Dim den2div As Integer = DivDen2.Text
  6.         Dim numansdiv As Integer = (num1div * den2div)
  7.         Dim denansdiv As Integer = (num2div * den1div)
  8.         Dim WholeNumberDiv As Integer
  9.         Dim ReduceFractionDiv = GCD(numansdiv, denansdiv)
  10.  
  11.         ' This Reduces the fraction down to the least common denominator using the function GCD
  12.         numansdiv = (numansdiv / ReduceFractionDiv)
  13.         denansdiv = (denansdiv / ReduceFractionDiv)
  14.  
  15.         ' After the math. If the numerator is higher that the denominatoer run this
  16.         If Math.Abs(numansdiv) >= Math.Abs(denansdiv) Then
  17.  
  18.             ' Setting variables
  19.             Dim LoopCountadd As Integer
  20.             Dim TimesLoopedadd As Integer
  21.             TimesLoopedadd = 0
  22.             LoopCountadd = 0
  23.  
  24.             ' A loop to subtract the denominator from the numerator until the numerator_
  25.             ' is smaller than the denominator
  26.             Do While Math.Abs(numansdiv) >= Math.Abs(denansdiv)
  27.                 ' Subtracts the numerator from the denominator
  28.                 TimesLoopedadd = (Math.Abs(denansdiv) - Math.Abs(numansdiv))
  29.                 ' Counts up 1 for every time the numerator is subtracted from the denominator
  30.                 LoopCountadd += 1
  31.                 ' Makes WholeNumber = the total of times the loop occurred
  32.                 WholeNumberDiv = LoopCountadd
  33.                 ' Makes the remainder = the new numerator answer
  34.                 numansdiv = (Math.Abs(numansdiv) - Math.Abs(denansdiv))
  35.  
  36.                 ' Goes back to see if the numerator is higher than the denominator
  37.             Loop
  38.  
  39.         End If
  40.  
  41.         ' If there is no fraction display only the whole number
  42.         If numansdiv = 0 Then
  43.  
  44.             DivFractAns1.Text = "" & (WholeNumberDiv)
  45.  
  46.             ' If there is no whole number then display the fraction only
  47.         ElseIf WholeNumberDiv = 0 Then
  48.  
  49.             DivFractAns1.Text = "" & (numansdiv) & "/" & (denansdiv)
  50.  
  51.             ' Otherwise the whole number and the remaining fraction are displayed
  52.         Else
  53.  
  54.             DivFractAns1.Text = "" & (WholeNumberDiv) & " and " _
  55.             & (numansdiv) & "/" & (denansdiv)
  56.  
  57.  
  58.  
  59.         End If
  60.  
Thx all
Nov 24 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

25
by: TK | last post by:
I'm used to programming in c or c++ in which my problem is simple. I want to be able to enter a value on a page (like 3.2), and then read it as a 32-bit float and break it into it's individual...
4
by: M. Uppal | last post by:
How do i convert fraction to time in c#. I need to convert 0.25 to time. Excel does this by using Selection.NumberFormat = "h:mm:ss;@" of the Cell Format function. thanks, M. Uppal
11
by: Penfold | last post by:
I'd appreciate help converting student average test scores into grades. My problem is that I need to allocate one of about 20 grades (3a,3b,3c,4a,4b,4c etc through to 8c plus a couple of others)....
5
by: Jeff Dillon | last post by:
How might I convert a string like 10.A (in hex) to it's decimal equivalent? Basically I have an input string like ((1F.A + 3A.D) - 1F.E) and need to calculate the result. Using Reflection and...
52
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I convert a Number into a String with exactly 2 decimal places?...
2
by: clintonb | last post by:
Victor said: The double value that I'm trying to convert to GCSMoney (which is implemented as cents) was produced by multiplying a dollar amount by an interest rate to get interest. double...
17
by: D'Arcy J.M. Cain | last post by:
I'm not sure I follow this logic. Can someone explain why float and integer can be compared with each other and decimal can be compared to integer but decimal can't be compared to float? True...
6
by: Terry Reedy | last post by:
Gerhard Häring wrote: The new fractions module acts differently, which is to say, as most would want. True Traceback (most recent call last): File "<pyshell#20>", line 1, in <module> F(1.0)...
10
by: Jason | last post by:
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.