Multiplying variables and doubles in swift
I’m a designer looking into learning Swift and I’m a beginner.
I have no experience whatsoever.
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
I get the error:
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?
- How does an underscore in front of a variable in a cocoa objective-c class work?
- PHP: “Notice: Undefined variable”, “Notice: Undefined index”, and “Notice: Undefined offset”
- for loop variable name use swift
- why can't I declare a variable like “newVariable” in Obj-C?
- Xcode debugger stopped displaying local and self variables
- What's the meaning of static variables in an implementation of an interface?
2 Solutions Collect From Internet About “Multiplying variables and doubles in swift”
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
If you declare
billBeforeTax like so..
var billBeforeTax = 100.0
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.
You just have to cast your int variable to Double as below:
var billBeforeTax = 100 var taxPercentage = 0.12 var tax = Double(billBeforeTax) * taxPercentage
- Only allow letters and numbers for a UITextField
- Swift 3; Range 'Out of bounds'
- Publish iOS app to App Store on behalf of client
- Need Help Integrating Interstitial ads in Spritekit iOS Game
- What's the difference between all the Selection Segues?
- Get Objective-C interface in Swift?
- Trying to handle “back” navigation button action in iOS
- Send data from an iPhone to a Web service
- Is this a new way to define private instance variables in Objective-C?
- How to play a video from an URL in iphone
- Unknown class in Interface Builder file. Xcode 6 and Swift
- How can I get the standard iPhone Copy bubble to appear on a UIImage?
- Crashing while trying to move UITableView rows
- realm cocoa: is it possible to do queries on links
- One viewcontroller for multiple UITabBar sections