Multiplication of variables and doubling in fast

I am a designer learning Swift, and I'm a beginner.

I have no experience.

I am trying to create a tip calculator using the base code in an Xcode playground.

Here is what I still have.

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

I get an error message:

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

Does this mean that I cannot multiply doubling?

Did I miss any basic concepts of variables and doubles here?

+7
variables double swift multiplying
source share
2 answers

You can use only a few two of the same data types.

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

If you declare billBeforeTax like that.

 var billBeforeTax = 100.0 

It will be interpreted as double, and multiplication will work. Or you can also do the following.

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

You just need to pass your int variable to Double, as shown below:

  var billBeforeTax = 100 var taxPercentage = 0.12 var tax = Double(billBeforeTax) * taxPercentage 
+1
source share

All Articles