Skip to content

Instantly share code, notes, and snippets.

@kiras0518
Created October 30, 2016 10:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kiras0518/fbbc318df8c67e258be2e6ce82d1ec92 to your computer and use it in GitHub Desktop.
Save kiras0518/fbbc318df8c67e258be2e6ce82d1ec92 to your computer and use it in GitHub Desktop.
//
// ViewController.swift
// Tipcalcultion
//
// Created by Feng on 2016/10/25.
// Copyright © 2016年 Fang. All rights reserved.
//
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBOutlet weak var cost: UITextField!
@IBOutlet weak var tip: UITextField!
@IBOutlet weak var result: UILabel!
@IBAction func compute(_ sender: AnyObject) {
if(cost.text == "" || tip.text == ""){
result.text = "請輸入金額!" //防呆使用
} else {
let thecost = Double(cost.text!) //原始金額
let thetip = Double(tip.text!)!/100 //除以百分比
result.text = String((thecost! * thetip)+thecost!) // (金額*小費) + 金額 = 最終金額
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment