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
- ' Converts the two fractions entered into decimals and adds them
- ' Then converts the Decimal answer back into a fraction
- Private Sub AddFract_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddFract.Click
- Dim AddFractSolve As Long
- Dim LargestDenominator As Long
- Dim GCD As Long
- Dim TopNumber As Long
- Dim Remainder As Long
- Dim WholeNumber As Long
- Dim Numerator As Long
- Dim Denominator As Long
- Dim DecimalNumber As Long
- AddFractSolve = (Val(AddNum1.Text) / Val(AddDen1.Text)) + (Val(AddNum2.Text) / Val(AddDen2.Text))
- If IsNumeric(AddFractSolve) Then
- AddFractSolve = CDbl(AddFractSolve)
- WholeNumber = Fix(AddFractSolve)
- Denominator = LargestDenominator
- Numerator = Format(Denominator * _
- Math.Abs(DecimalNumber - WholeNumber), "0")
- If Numerator Then
- GCD = LargestDenominator
- TopNumber = Numerator
- Do
- Remainder = (GCD Mod TopNumber)
- GCD = TopNumber
- TopNumber = Remainder
- Loop Until Remainder = 0
- Numerator = Numerator \ GCD
- Denominator = Denominator \ GCD
- AddFractAns1.Text = "" & (WholeNumber) & _
- "and" & _
- (Numerator) & "/" & _
- (Denominator)
- Else
- AddFractAns1.Text = (WholeNumber)
- End If
- Else
- End If
- End Sub
Expand|Select|Wrap|Line Numbers
- Function MakeFraction(ByVal DecimalNumber As Variant, _
- Optional ByVal LargestDenominator As Long = 64, _
- Optional bShowDash As Boolean = False) As String
- Dim GCD As Long
- Dim TopNumber As Long
- Dim Remainder As Long
- Dim WholeNumber As Long
- Dim Numerator As Long
- Dim Denominator As Long
- If IsNumeric(DecimalNumber) Then
- DecimalNumber = CDbl(DecimalNumber)
- WholeNumber = Fix(DecimalNumber)
- Denominator = LargestDenominator
- Numerator = Format(Denominator * _
- Abs(DecimalNumber - WholeNumber), "0")
- If Numerator Then
- GCD = LargestDenominator
- TopNumber = Numerator
- Do
- Remainder = (GCD Mod TopNumber)
- GCD = TopNumber
- TopNumber = Remainder
- Loop Until Remainder = 0
- Numerator = Numerator \ GCD
- Denominator = Denominator \ GCD
- MakeFraction = CStr(WholeNumber) & _
- IIf(bShowDash, "-", " ") & _
- CStr(Numerator) & "/" & _
- CStr(Denominator)
- Else
- MakeFraction = CStr(WholeNumber)
- End If
- Else
- 'Input wasn't a number, handle error here
- End If
- End Function
Thank you'
Semajthewise