我有密码:

@IBOutlet weak var summaryLbl: UILabel!
var actualNumber: Double = 0
var previousNumber: Double = 0
var operationMath: Bool = false
var operation = 0


@IBAction func numberPressed(_ sender: UIButton) {
    if operationMath == true
    {
        summaryLbl.text = String(sender.tag)
        actualNumber = Double(summaryLbl.text!)!
        operationMath = false
    }
    else
    {
        if summaryLbl.text == "0" {
            summaryLbl.text = ""
        }
        summaryLbl.text = summaryLbl.text! + String(sender.tag)
        actualNumber = Double(summaryLbl.text!)!
    }
}

@IBAction func buttons(_ sender: UIButton) {
    if summaryLbl.text != "" && sender.tag != 10 && sender.tag != 17
    {
        previousNumber = Double(summaryLbl.text!)!

        if sender.tag == 13
        {
            summaryLbl.text = "/";
        }
        else if sender.tag == 14
        {
            summaryLbl.text = "x";
        }
        else if sender.tag == 15
        {
            summaryLbl.text = "-";
        }
        else if sender.tag == 16
        {
            summaryLbl.text = "+";
        }

        operation = sender.tag
        operationMath = true;
    }
    else if sender.tag == 17
    {
        if operation == 13
        {
            let summary = rounding(number: previousNumber / actualNumber)
            summaryLbl.text = summary
        }
        else if operation == 14
        {
            let summary = rounding(number: previousNumber * actualNumber)
            summaryLbl.text = summary
        }
        else if operation == 15
        {
            let summary = rounding(number: previousNumber - actualNumber)
            summaryLbl.text = summary
        }
        else if operation == 16
        {
            let summary = rounding(number: previousNumber + actualNumber)
            summaryLbl.text = summary
        }
        else if operation == 11 //TODO: negative numbers
        {
            let summary = rounding(number: previousNumber + actualNumber)
            summaryLbl.text = summary
        }
        else if operation == 12 //TODO: percentages
        {
            let summary = rounding(number: (previousNumber * actualNumber) / 100)
            summaryLbl.text = summary
        }
    }
    else if sender.tag == 10
    {
        summaryLbl.text = "0"
        previousNumber = 0;
        actualNumber = 0;
        operation = 0;
    }

}
override func viewDidLoad() {
    super.viewDidLoad()
    summaryLbl.text = "0"
    previousNumber = 0;
    actualNumber = 0;
    operation = 0;
}

func rounding (number: Double) -> String {
    let numberAfterRounding = String(number)
    return numberAfterRounding
}

我在做一个小计算器。
我有个问题:
我总是得到一个结果,如5.0,25.0,18.0(提示:.0)-如何摆脱它?
如何做加法/负数?在按下按钮operation==11之后,我希望数字变为负数,或者如果是负数,则为正数(就像在iPhone中使用苹果计算器一样)
百分比对我来说不太管用:(

最佳答案

问题1的答案:
假设您的结果是3.0,您只想显示"3"而不是"3.0",对于1.23,您想显示"1.23"。首先对数字进行舍入,检查舍入的数字和结果是否相同。如果它们相同,则将其转换为Int并进行插值。
这是密码。

let result: Double = 3.0
let rounded = result.rounded()
if (result == rounded) { yourLabel.text = "\(Int(result))" }
else { yourLabel.text = "\(result)" }

问题2的答案:
Swift具有negate()功能,它完全符合您的要求。
var result: Double = 30
result.negate() // it's now -30
result.negate() // it's now 30 again

问题3的答案:
我想当你说百分比的时候,你是指模数。
在swift中,不能对浮点(如3.0 % 1.0)使用%(模运算符)。相反,您必须使用truncatingRemainder(dividiedBy:)函数。
result.truncatingRemainder(dividedBy: 10) // can be any number you like

关于ios - 小型计算器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51395447/

10-13 06:35