This is the function I'm using now for converting a value of of one unit to another. I'm looking for a more dynamic option of doing this. I want to make the function be able to covert from for example: 1 kilo gram to ounces, but the why I'm doing this now I have defined how much each unit is compared to another. is there a simpler way to do this?
func weightMath(_ input: UITextField, _ textField: UITextField) {
//The math for doing weight calculations. This will also round up the answer to the third decimal.
switch input.tag {
/*
case 0 is for when the user want to calculate from kilogram
case 1 is for when the user want to calculate from ounces
case 2 is for when the user want to calculate from pounds
*/
case 0:
switch textField.tag {
/*
case 0 is for when the user want to calculate to kilogram
case 1 is for when the user want to calculate to ounces
case 2 is for when the user want to calculate to pounds
*/
case 0:
textField.text = String(format: "%.3f", toFloat(input.text!) * 1)
case 1:
textField.text = String(format: "%.3f", toFloat(input.text!) * 35.274)
case 2:
textField.text = String(format: "%.3f", toFloat(input.text!) * 2.205)
default:
break
}
case 1:
switch textField.tag {
/*
case 0 is for when the user want to calculate to kilogram
case 1 is for when the user want to calculate to ounces
case 2 is for when the user want to calculate to pounds
*/
case 0:
textField.text = String(format: "%.3f", toFloat(input.text!) * 0.028)
case 1:
textField.text = String(format: "%.3f", toFloat(input.text!) * 1)
case 2:
textField.text = String(format: "%.3f", toFloat(input.text!) * 0.063)
default:
break
}
case 2:
switch textField.tag {
/*
case 0 is for when the user want to calculate to kilogram
case 1 is for when the user want to calculate to ounces
case 2 is for when the user want to calculate to pounds
*/
case 0:
textField.text = String(format: "%.3f", toFloat(input.text!) * 0.454)
case 1:
textField.text = String(format: "%.3f", toFloat(input.text!) * 16)
case 2:
textField.text = String(format: "%.3f", toFloat(input.text!) * 1)
default:
break
}
default:
break
}
}