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?
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
- How to detect if any external libraries are calling uniqueIdentifier]?
- lldb fails to print variable values with “error: reference to 'id' is ambiguous”
- Measuring cellular signal strength
- Best option for supplying Quadtree GPS Data to an app?
- Bluetooth video streaming in WiFiCameraApp
- Google SignIn: Unable to disconnect iOS app
- how could two different View Controllers Communicate with each other
- iOS: “error in __connection_block_invoke_2: Connection interrupted” but app only uses < 20 MB of memory
- iOS 8 removing build from review in iTunesConnect
- get the array index in for statement in objective-c
- any option to know if apple app get the push notification?
- How can I link to my app in the App Store (iTunes)?
- iOS: Drawing a Rectangle on an ImageView and Adjusting Borders
- Xcode: Error Domain=DVTPlaygroundCommunicationErrorDomain Code=1
- Total RAM in iPhone