Ratio calculator - Help

Can anyone code me a ratio Calculator.

eg:


100:50

2:1


Also if the number has decimals it need to be rounded down.


All Number are divisable by 2,3,5,7



Thank You

Replies

What do you mean by ratio calculator ?


Do you mean reduce the quotient factors to the smallest form ?


You say :

All Number are divisable by 2,3,5,7

Do you mean divisible by all those numbers

That's not the case for 100

or at least one ?

Anyway, this has no importance.


If you need to reduce the quotient factors, you can use this code (gcd is the greatest common divisor)


func gcd (_ n1: Int, _ n2: Int) -> Int {
    if n2 != 0 {
    return gcd(n2, n1 % n2)
    } else {
    return n1
    }
}

func reduceQuotient(_ a: Int, _ b: Int) -> (aReduced: Int, bReduced: Int) {
    var aReduced = a
    var bReduced = b
   
    if b == 0 {
        print("\(a) : \(b) is not a fraction")
            return (a, b)
    }
   
    let greatestCommonDivisor = gcd(a, b)
    aReduced = a / greatestCommonDivisor
    bReduced = b / greatestCommonDivisor

    return (aReduced, bReduced)
}


Use as:

let (x, y) = reduceQuotient(100, 50)
print(x, ":", y)

result:

2 : 1

Thank You.

It needs to be something like this

https://www.calculatorsoup.com/calculators/math/ratios.php


What i mean is i have to make an interface on the phone on xcode. Then beable to code "how to simplify ratio". Something like what you've done. But it also needs to be able to do decimals (Which is just rounding down to the whole number.


eg;

// test For Decimal


// divide value


//round result down set as roundedResult


//if result - rounderResult+ >0 "print "Decimal Exits


//else "print" no decimal


I've coppied your code on playground and it works Thank you! But if i do something like 221:43 it doesnt work. Is it possible to make it work ?


I hoping to make it like that website above.





Thanks again.

What do you mean if i do something like 221:43 it doesnt work.


I've tried it gives :

221 : 43


That's good answer : 43 is prime number and 221 is not a multiple of 43 (in fact it is 13 * 17)

So, the quotient cannot be simplified. What result did you expect ?


I tried the calculator php, it gives :

Answer:

221 : 43 = 442 : 86
The ratio is already in lowest terms so we found an equivalent ratio by multiplying both terms by 2.

If you multiply, you could give any result, as 663 : 129, but that's nonsense and not your initial spec.

.

it also needs to be able to do decimals

Do you mean, for 221:43, give you rounding as 5.14 ? How many decimals

That's very simple: (for 2 decimals)

let r = Float(a) / Float(b)
let result = String(format: "%.2f", r)



What is your problem ? Designing the view in Interface Builder ?

Forget what i said before.


The code you sent before works only on playground. How do you make it work for "xcode project"


i know print only works in playground, but i dont know how to fix it it in xcode project



Should be a photo above!

No, print works as well in code, but it prints to the console.


In code, the func are the same.


To output the result :


- create the IBOutlet for the inputs

     @IBOutlet var numeratorField: UITextField!
     @IBOutlet var denominatorField: UITextField!

- define a label in the ViewController, in Interface Builder

- connect to an IBOutlet

     @IBOutlet var resultLabel: UILabel!

- create a button asking to Reduce

- connect to an IBAction where you generate the output


@IBAction func calcReuced(_ sender: UIButton) {
     if let a = Int(numeratorField.text) , let b = Int(denominatorField)  {
          let (x, y) = reduceQuotient(a, b)
          resultLabe = "\(x), ":", \(y)"
     }
}



Note: no photo possible on the forum.


I advise you to read carefully (if you did not do it yet) Intro to App development on iBooks store.

So the last two bottom lines of codes are coming up with an error.



let (x, y) = reduceQuotient (a, b) for this line the error is "immutable value "y" was never used". I think its because the y down below is still red not black.



resultLabel = "\(x), ":", \(y)"

There are three errors here:

1st: cannont assign value of "String" to type "UILabel"

2nd: saying it should be seperated by ";" (which i don't think is right)

3rd: expected expression (Which i think its the y, because the y is red)





Heres what i have


@IBOutlet var firstField: UITextField! ( i change numerater and denominator to first and second)

@IBOutlet var secondField: UITextField!

@IBOutlet var resultLabel: UILabel!



@IBAction func calcReduce(_ sender: UIButton) {

if let a = Int(firstField.text!) , let b = Int(secondField.text!) {


let (x, y) = reduceQuotient (a, b)

resultLabel = "\(x), ":", \(y)"



Thanks

Sorry for some typing errors


Should be:


@IBAction func calcReduced(_ sender: UIButton) { 
     if let a = Int(numeratorField.text) , let b = Int(denominatorField)  { 
          let (x, y) = reduceQuotient(a, b) 
          resultLabel!.text = "\(x) : \(y)" 
     } 
}