'How to get value as int if the button title in in terms of percentage?

This is the application to calculate tip and split it between number of selected users.

In the screenshot below, I've 3 buttons under "Select tip" section. I want to use the values as int for calculating the tip. How do I get the value as int?

For eg. If I select 10% button, I want only 10 as the value for carrying out further calculations.

Simulator Screen Shot - iPhone 13 Pro - 2022-03-14 at 21.51.48strong text



Solution 1:[1]

I found the following solution to work best in my case.

@IBAction func tipChanged(_ sender: UIButton) {
    
    tipSelected = sender.currentTitle!
    
    let tipSelectedMinusPer = tipSelected.dropLast()
    print(tipSelectedMinusPer)
}

dropLast() function deletes the last character from the string. So, basically we drop the % symbol in this case and are left behind with plain number as value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Aditya Vyavahare