This was a fun project. I added percentages and winner/tie labels. I also added a tie score label and a reset button.
The hardest part was trying to figure out how to display the decimal numbers correctly. I tried using tuncatingRemainder(Dividing by) to tell if the float number for the percentages was actually a whole number. It worked on most whole numbers but missed 60% and 40% to name a few. The problem was that lets say (3/5)*100 should equal 60. However the computer was calculating the answer to 60.000004. to get around this I had to round the float to one decimal and then compare the INT value to the rounded float value. (see below)
// Convert Int values to Float values, calculate percent and format the output to look like "90.1%" or "90%"
func calculatePercentage(_ score:Int, _ total:Int) -> String {
let percent:Float
let percentRounded:Float
let answer:String
// calculate percent and convert Integers to Float values
percent = (Float(score) / Float(total)) * 100
//Round percent to one decimal Note: Rounding to one decimal was necessary because some values like 40 or 60 were being calculated as 60.000004 or 40.000004. so they were falling through to the else statement.
percentRounded = round(10 * percent)/10
if percentRounded == Float(Int(percent)) {
// Calculated value is an integer(Whole Number), round to zero decimals
answer = "\(String(format: "%.0f" , percent))%"
}
else {
// Calculated value is not an integer(Decimal number), round to one decimal
answer = "\(String(format: "%.1f" , percent))%"
}
return answer