#14 股票交易手續費計算機
Published in
6 min readAug 7, 2021
class ViewController: UIViewController {@IBOutlet weak var buyPriceTextField: UITextField!@IBOutlet weak var StockVolumeTextField: UITextField!@IBOutlet weak var StockCommissionTextField: UITextField!@IBOutlet weak var sellPriceTextField: UITextField!@IBOutlet weak var buyCostLabel: UILabel!@IBOutlet weak var stockCommissionStepper: UIStepper!@IBOutlet weak var stockVolumeStepper: UIStepper!@IBOutlet weak var sellTaxLabel: UILabel!@IBOutlet weak var sellCostLabel: UILabel!@IBOutlet weak var totalCostLabel: UILabel!@IBOutlet weak var daytradeSwitch: UISwitch!@IBOutlet weak var stockView: UIView!let stockCost:Double = 0.1425 //手續費 買進賣出0.1425%var stockTax:Double = 0.003 // 交易稅 0.3%var dayTradeTax:Double = 0.0015 //當沖交易稅減半var buyPrice:Double = 0.0 //買進價var stockVolume:Int = 0 //買進股數var stockdiscount:Double = 10 //券商折扣,預設10為沒有折扣var sellPrice:Double = 0.0 //買出價var buyCost:Double = 0.0 //買進成本var sellCost:Double = 0.0 //賣出成本var sellTax:Double = 0.0 //證交稅var totalCost:Double = 0.0 //總交易成本override func viewDidLoad() {super.viewDidLoad()buyPriceTextField.text = "100"StockVolumeTextField.text = "1000"StockCommissionTextField.text = "2.8"sellPriceTextField.text = "100"stockView.layer.cornerRadius = 15daytradeSwitch.isOn = false //當沖開關預設關閉// Do any additional setup after loading the view.}@IBAction func acountVolume(_ sender: Any) {StockVolumeTextField.text = String(format:"%.0f",stockVolumeStepper.value)}@IBAction func acountDiscount(_ sender: Any) {StockCommissionTextField.text = String(format:"%.1f",stockCommissionStepper.value)}@IBAction func cal(_ sender: Any) {buyPrice = Double(buyPriceTextField.text!) ?? 0stockVolume = Int(StockVolumeTextField.text!) ?? 0stockdiscount = Double(StockCommissionTextField.text!) ?? 0sellPrice = Double(sellPriceTextField.text!) ?? 0//計算買進手續費buyCost = (buyPrice * Double(stockVolume) * stockCost * stockdiscount)/1000buyCostLabel.text = String(format:"%.1f", buyCost) //顯示買進手續費//計算賣出手續費sellCost = (sellPrice * Double(stockVolume) * stockCost * stockdiscount) / 1000sellCostLabel.text = String(format: "%.1f", sellCost) //顯示賣出手續費//計算證交稅 判斷是否當沖交易if daytradeSwitch.isOn {sellTax = sellPrice * dayTradeTax * Double(stockVolume)} else {sellTax = sellPrice * stockTax * Double(stockVolume)}sellTaxLabel.text = String(format: "%.1f", sellTax) //顯示證交稅//買買交易總成本totalCost = buyCost + sellCost + sellTaxtotalCostLabel.text = String(format: "%.1f", totalCost)view.endEditing(true) //收鍵盤}}