二元运算符 * 不能应用于 Int 和 Double 类型的操作数 [英] binary operator * cannot be applied to operands of type Int and Double

查看:66
本文介绍了二元运算符 * 不能应用于 Int 和 Double 类型的操作数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个简单的 Swift 应用来计算增值税(增值税 = 20%).

I'm trying to build a simple Swift app to calculate VAT (Value Added taxes = 20%).

func taxesFree(number: Int) -> Double {

    var textfield = self.inputTextField.text.toInt()!
    let VAT = 0.2
    var result = textfield * VAT

    return result        
}

出于某种原因,我不断收到

For some reason I keep getting

二元运算符 * 不能应用于 Int 和 Double 类型的操作数

Binary operator * cannot be applied to operands of type Int and Double

上线

var result = textfield * VAT    

推荐答案

你应该将一种类型转换为另一种类型,这样两个变量应该是相同的类型:

You should convert one type to the other one so both variable should be the same types:

var result: Double = Double(textfield) * VAT

这篇关于二元运算符 * 不能应用于 Int 和 Double 类型的操作数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆