goodfoy
(Bechir Mihoub)
1
Im trying to calculate percentage for the questions I have right and my percentage is always 0 or 100. Help me get the right percentage.
initialized
var percentage = 0
@IBAction func getScore(_ sender: UIButton) {
questionsTotal = myAnswerArray.count
print ("This is how many questions I have (questionsTotal)")
for oneAns in myAnswerArray {
if oneAns.message == "Your correct
" {
questionsRight = questionsRight + 1
}
}
print("This is how many questions I have right (questionsRight)")
percentage = questionsRight/questionsTotal * 100
print(percentage)
percentageString = String(percentage)
self.percentageStringUsed = percentageString + "%"
performSegue(withIdentifier: "passRes", sender: self)
}
please help me.
Nevin
2
You’re using integer division, you need floating-point division.
Or multiply by 100 first before dividing, if you want it rounded down.
Or, um, multiply by 200, add the denominator, then divide by twice the denominator.
1 Like
goodfoy
(Bechir Mihoub)
3
I need an integer answer how would I go about doing that?