Hello mighty StackOverflowers,
我知道这个问题肯定会超过一百次,但我只是在这个阶段作为一个初学者(Swift 4 / Xcode 9),我完全不知道如何实现所有这些提示和我发现的东西,但是。
我觉得这个很简单,所以这是我的神经痛:
我为百分比创建了一个小应用程序。一切正常......只要我不能触摸我的iPhone设置为德语的数字键盘上的逗号,因为计算仅适用于点分隔符。
在开始时,我希望将UITextField转换为以点分隔的Integer / Float,并给出结果我想将其转换回逗号分隔的数字。 短:用逗号分隔符输入,用点分隔符计算,"打印"结果显示逗号分隔符。
只要电话设置为英语或(如果是德语),只有非十进制数字被添加到字段中,这里的代码目前正在运行:
哦......还有一件重要的事情:我很难将迄今为止我在这里找到的代码片段添加到我的ViewController.swift中......我只是不知道将这些行放在哪里:( 我们走了:
class ViewController: UIViewController {
//User Fields:
@IBOutlet weak var kind: UITextField! // kind of unit (€, $, metres...)
@IBOutlet weak var base: UITextField! //base
@IBOutlet weak var percent: UITextField! //percent (1%, 4,5% etc.)
//Result Labels:
@IBOutlet weak var percentInKind: UILabel! //the percent as value in kind
@IBOutlet weak var total: UILabel! //base minus percentInKind
//Calculation:
@IBAction func calculateTotal(_ sender: Any) {
let kindConv = String(kind.text!) //Converted to String
let baseConv = Double(base.text!) //Converted to Double
let percentConv = Double(percent.text!) //Converted to Double
let percentInKindConv = Double(baseConv! * percentConv!) / 100
percentInKind.text = String(format: "%.2f", percentInKindConv) + " \(kindConv)"
total.text = String(format: "%.2f", baseConv! - percentInKindConv) + " \(kindConv)"
}
答案 0 :(得分:0)
//User input fields
@IBOutlet weak var kind: UITextField! //kind of unit (€, metres etc.)
@IBOutlet weak var base: UITextField! //base number
@IBOutlet weak var percent: UITextField! //percent in %
//Result labels
@IBOutlet weak var sumPercent: UILabel! //% in kind of unit
@IBOutlet weak var total: UILabel! //base - sumPercent
//Calculation
@IBAction func berechnenAbziehen(_ sender: Any) {
let kindVar = String(kind.text!)
let formatter = NumberFormatter()
formatter.numberStyle = .decimal
formatter.alwaysShowsDecimalSeparator = true
formatter.locale = Locale(identifier: "de_DE")
formatter.decimalSeparator = ","
formatter.maximumFractionDigits = 2
formatter.minimumFractionDigits = 2
let baseFormatter = formatter.number(from: base.text!) as! Double
let percentFormatter = formatter.number(from: percent.text!) as! Double
let percentSum = baseFormatter * percentFormatter / 100
let totalSum = baseFormatter - percentSum
sumPercent.text = formatter.string(for: percentSum)! + " \(kindVar)"
total.text = formatter.string(for: totalSum)! + " \(kindVar)"
晚安,晚安! ; - )