Ilya Leshkinov - 2 years ago 71
Swift Question

# iOS SWIFT Propotional values calculation

I have a func that recives first value and should return another one

the input range is 24 - (8 - 0) (Int, not more and not less)

and output should give 20 - 60

so 24 will give 20 and (all <= 8) will give 60
and ll others propotional to each other

How can i reach such a calculation?

I have made method in objective c, keep main logic from it and convert to swift,

`````` - (int)calculate : (int)parameter{

int returnParameter;

if (parameter < 8) {

returnParameter = 60;
}
else{

returnParameter = 20 + ((24 - parameter) * 2.5);

}

NSLog(@"return value : %d",returnParameter);

return returnParameter;

}
``````

Pass parameter between 0 to 24 and it will return 60 to 20 respectively as per your need.

You should use double or float for more accuracy! because this will convert in integer if fraction is there!

Update in swift :

something like this,

``````func GetValue(param: Int) -> Int
{
var Ret = Int()

if param < 8
{
Ret = 60
}else{

Ret = 20 + Int(Double(24 - Double(param)) * 2.5)
}

if param > 24
{
Ret = 8
}

return Ret
}
``````