乘以变量并迅速加倍 [英] Multiplying variables and doubles in swift

查看:88
本文介绍了乘以变量并迅速加倍的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名学习Swift的设计师,并且是初学者。

I'm a designer looking into learning Swift and I'm a beginner.

我什么都没有经验。

我正在尝试在Xcode的游乐场中使用基本代码创建一个小费计算器。

I'm trying to create a tip calculator using basic code in Xcode's playground.

这是我到目前为止所拥有的。

Here is what I have so far.

var billBeforeTax = 100
var taxPercentage = 0.12
var tax = billBeforeTax * taxPercentage

我得到错误:


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

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

这是否意味着我不能将双精度数相乘?

Does this mean I can't multiply doubles?

我在这里错过了变量和双精度的基本概念吗?

Am I missing any of the basic concepts of variables and doubles here?

推荐答案

您只能使用相同数据类型的两个。

You can only multiple two of the same data type.

var billBeforeTax = 100 // Interpreted as an Integer
var taxPercentage = 0.12 // Interpreted as a Double
var tax = billBeforeTax * taxPercentage // Integer * Double = error

如果您声明 billBeforeTax 像这样。

var billBeforeTax = 100.0

它将被解释为Double,并且乘法将起作用。或者,您也可以执行以下操作。

It will be interpreted as a Double and the multiplication will work. Or you could also do the following.

var billBeforeTax = 100
var taxPercentage = 0.12
var tax = Double(billBeforeTax) * taxPercentage // Convert billBeforeTax to a double before multiplying.

这篇关于乘以变量并迅速加倍的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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