alex

I'm doing a fraction calculator and I'm trying to add 2 fractions when I put in my fractions it just adds my Whole numbers only and nothing else if my fraction is 2 3/4 + 2 3/5 it add the whole numbers and outputs 4

```
var firstStep = firstDenomInTextField! * firstWholeInTextField! / firstDenomInTextField!
var secondStep = firstStep + firstNumInTextField! / firstDenomInTextField!
var thirdStep = secondDenomInTextField! * secondWholeInTextField! / secondDenomInTextField!
var fourthStep = thirdStep + secondNumInTextField! / secondDenomInTextField!
var calculatedAnswer = (secondStep + fourthStep)
var numerator = Int(calculatedAnswer * 10 * 10)
println(numerator)
answerLabel.hidden = false
answerLabel.text = printSimplifiedFraction(Numerator: numerator)
```

**printSimplifiedFraction Function**

```
func printSimplifiedFraction(Numerator numerator: Int, Denominator denominator: Int = 100) -> String
{
var finalNumerator = numerator;
var finalDenominator = denominator;
var wholeNumbers:Int = numerator / denominator;
var remainder:Int = numerator % denominator;
//println("wholeNumbers = \(wholeNumbers), remainder = \(remainder)");
//println("\(denominator) % \(remainder) = \(denominator % remainder)");
if(remainder > 0)
{
// see if we can simply the fraction part as well
if(denominator % remainder == 0) // no remainder means remainder can be simplified further
{
finalDenominator = denominator / remainder;
finalNumerator = remainder / remainder;
}
else
{
finalNumerator = remainder;
finalDenominator = denominator;
}
}
if(wholeNumbers > 0 && remainder > 0)
{
// prints out whole number and fraction parts
return("Simplified fraction of \(numerator)/\(denominator) = \(wholeNumbers) \(finalNumerator)/\(finalDenominator)");
}
else if (wholeNumbers > 0 && remainder == 0)
{
// prints out whole number only
return("Simplified fraction of \(numerator)/\(denominator) = \(wholeNumbers)");
}
else
{
// prints out fraction part only
return("Simplified fraction of \(numerator)/\(denominator) = \(finalNumerator)/\(finalDenominator)");
}
}
```

**My Question** I want to make it so it does not just add the whole numbers but add the whole fraction. If you need any clarifications or questions please comment them down below

Leo Dabus

If you are working with fractions, you should use Double instead of Int. Also when multiplying by 10 you should use 10.0 instead. Be careful you are mixing Int(Integers) with Double(fractions). Also when declaring vars as parameters if you omit it will be a constant by default, if you would like to change it you don't need a second var, just add var in front of it when declaring it there). I think You should restart it from the beginning, Syntax is OK. Don't forget to convert from Int to Double when needed.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at

*1*### golang http client and server can't maintain reliable connection

*2*### pump.io port in URL

*3*### Selenium browsing with headless isn't working?

*4*### How i extract text from a model dialog in selenium?

*5*### How to update if key exists - sequelize

*6*### change TabView indicator SwiftUI

*7*### When does a running Docker container run out of disk space?

*8*### Java POI: How to find an Excel cell with a string value and get its position (row) to use that position to find another cell

*9*### Adding custom buttons to Kendo grid toolbar in ASP.NET Core

*10*### Curl function cannot parse proxy coming from a variable in bash

*11*### reduce the time complexity of nested for loop

*12*### CALayer with NSScrollView, zooming panning and clicking

*13*### python: for-loop doesn't differentiate actions

*14*### What is the difference between .NET Framework 4.6.1 Targeting Pack and .NET Framework 4.6.1 Runtime?

*15*### Creating interaction sessions based on timestamps in pandas

*16*### Spring JpaRepository - Detach and Attach entity

*17*### Calculate average of every n rows from a csv file

*18*### Set the "test" profile when run "mvn test"

*19*### How to configure CAPTCHA in Drupal

*20*### Insert Null value with pyodbc

*21*### Typescript Error - must have a '[Symbol.iterator]()' method that returns an iterator

## Comments