Yadiel Velez - 1 year ago 64
iOS Question

# Issues with tip calculator app and displaying data

Hey everyone I need some help here with my code
When I select the default tip rate in settings and go back the percentage label appear to be multiplied by 100.
I send an email to a professor and he told me this:

(("It looks like it's because you are saving the default tip rate as "20" instead of "0.20" so when you multiply that by "100" you get "2000" instead of "20"."))) There are a couple ways to solve this, but the easiest would be to divide by 100 when you compute the tip amount.

I will appreciate someones help me making the line of code that I need to divide the amount by 100.
Below I will leave some screenshots of my code.
The screenshot of the Views are in order :

Here is my MainViewController code

``````import UIKit
class ViewController: UIViewController, SettingsDelegate {
// Inputs
@IBOutlet weak var amountTextField: UITextField!
//Labels
@IBOutlet weak var TipPercentageLabel: UILabel!
@IBOutlet weak var numberOfPersonLabel: UILabel!
@IBOutlet weak var tipAmountLabel: UILabel!
@IBOutlet weak var totalBillLabel: UILabel!
@IBOutlet weak var billPerPersonLabel: UILabel!
//Slider & Stepper
@IBOutlet weak var tipSlider: UISlider!
@IBOutlet weak var personsStepper: UIStepper!
//Variables
var tipPercentage : Double = NSUserDefaults.standardUserDefaults().doubleForKey("DefaultTipRate") ?? 0.20
var numberOfPerson:Int = 1
let numberFormatter:NSNumberFormatter = NSNumberFormatter()

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
tipAmountLabel.text = "\$0.00"
totalBillLabel.text = "Bill Total"
billPerPersonLabel.text = "\$0.00"
numberOfPersonLabel.text = "1"
self.amountTextField.becomeFirstResponder()

}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func setupContainer() {

tipSlider.minimumValue = 0
tipSlider.maximumValue = 100
tipSlider.value = 20
tipSlider.addTarget(self, action: "sliderTipChanged:", forControlEvents: .ValueChanged)

personsStepper.minimumValue = 1
personsStepper.maximumValue = 30
personsStepper.value = 1
personsStepper.addTarget(self, action: "sliderPersonChanged:", forControlEvents: .ValueChanged)

amountTextField.text = ""

refreshCalculation()

}

@IBAction func OnEditingFieldBill(sender: AnyObject) {

refreshCalculation()
}

func refreshCalculation() {

numberFormatter.numberStyle = NSNumberFormatterStyle.DecimalStyle
if let amount = numberFormatter.numberFromString(amountTextField.text!) as? Double {

let tipAmount = amount * tipPercentage
let totalBill = amount + tipAmount
let billPerPerson = totalBill / Double(numberOfPerson)
numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
tipAmountLabel.text = numberFormatter.stringFromNumber(tipAmount)
totalBillLabel.text = numberFormatter.stringFromNumber(totalBill)
billPerPersonLabel.text = numberFormatter.stringFromNumber(billPerPerson)

} else {

tipAmountLabel.text = "-"
totalBillLabel.text = "-"
billPerPersonLabel.text = "-"
}

numberFormatter.numberStyle = NSNumberFormatterStyle.PercentStyle
numberFormatter.minimumFractionDigits = 1
numberFormatter.maximumFractionDigits = 1
TipPercentageLabel.text = self.numberFormatter.stringFromNumber(tipPercentage)

numberOfPersonLabel.text = "\(numberOfPerson)"

}

@IBAction func sliderTipChanged(sender: UISlider) {

tipPercentage = Double(round(tipSlider.value)) / 100
refreshCalculation()
}

@IBAction func StepperPersonChanged(sender: UIStepper) {
numberOfPerson = Int(round(personsStepper.value))
refreshCalculation()
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let SettingsViewController = segue.destinationViewController as? SettingsViewController {
SettingsViewController.delegate = self
refreshCalculation()
}
}
func tipPercentageChanged(newValue: Double) {
TipPercentageLabel.text = "\(newValue)%"
tipPercentage = newValue
refreshCalculation()
}
}
``````

And here are the Settings View code

``````import UIKit
protocol SettingsDelegate{
func tipPercentageChanged(newValue : Double)
}
class SettingsViewController: UIViewController {
@IBOutlet weak var tipControl: UISegmentedControl!
var tipRates: Double?
var destName : String!
var delegate :SettingsDelegate?

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAct
``````

Answer Source

In the Settings View it should be sufficient to change, since you have already given it a correct name of a rate, you should also make it a rate

``````var tipRate = [5, 10, 15, 20, 25, 30]
``````

to

``````var tipRate = [0.05, 0.10, 0.15, 0.20, 0.25, 0.30]
``````

You can also get rid of the next line which does the cast to Double, since it is not needed.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download