Multiplying Variables and Doubles in Swift

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.

Multiplication of Integer by double and storing result as a double

In Swift, you must explicitly convert types, instead of cast it. See the document.

Because each numeric type can store a different range of values, you must opt in to numeric type conversion on a case-by-case basis. This opt-in approach prevents hidden conversion errors and helps make type conversion intentions explicit in your code.

When you have b:Int, you can Double(b), because it's conversion from Int to Double, but you cannot b as Double or c: Double = b because it's casting.

As for var d:Double = a * b, The problem is that there is no * operator implementation which accepts Double and Int as operands and returns Double.

If you want, you can implement overloaded operator:

func *(lhs:Double, rhs:Int) -> Double {
return lhs * Double(rhs)
}

var d:Double = a * b // -> 14

But I don't recommend to do that. You should do explicit conversion instead.

Swift: Error when multiplying four Double variables

findOutput does not have a return type of Double. If you want to return a double from it, you need to declare the return type correctly. Also, you don't need all of those getter/setter methods in the second class - you can just use the dot operator to access properties. Finally, 'multiplier' is misspelled in your return call. So:

func findOutput() -> Double {
return information.volumeAmount * information.measurement.multiplier * information.concentration * information.compoundMolarMass
}

Understanding the Doubles and Ints in Swift

As you know, Swift is very strict with types, but there's one area where it's not so strict - literals. Double conforms to ExpressibleByIntegerLiteral, so you could do:

let x: Double = 1 // "1" is "magically" converted to a Double!?

and have it compile. The same with arrays - the compiler thinks that the array literal that you have:

[50, 5.0, 10]

is a [Double], because it can convert both 50 and 10 to Double. It can't be an [Int] because 5.0 can't be converted to an Int (Int does not conform to ExpressibleByFloatLiteral)

The line:

total += amounts[i]

only works when both sides are of the same type. Note that here, the compiler will not try to convert from Int to Double because the expressions involved (total and amounts[i]) are not literals!

If you change the array literal to [50, 10, 10], all elements are Int, so the compiler infers the array to be [Int], and amount[i] becomes an Int, causing the line to fail compilation.

Using and multiplying variables in View Controller

In your C++ code your variables are already in numeric datatype - Double.

The value that you will be getting back form NSFormCell is String.

You will need to get the value of the String into a numeric datatype.

let odds1 = NSNumberFormatter().numberFromString(imput1.text)?.doubleValue

Edit after code updated:

The code you shared has a number of conceptual problems in it. The best way i can help you to overcome these is to point you in the direction of an example of a basic calculator in swift.

http://www.ioscreator.com/tutorials/calculator-tutorial-in-ios8-with-swift

Multiplying double with int in Objective-C

First of all, I'm pretty sure you mean

double one = 465654753464353;

With the asterisk sign you create a pointer to a double.

So try this:

double one = 465654753464353;
int two = 4;
double sum = one * two;

This should work.

If you meant to use pointers, have a look at @JAB's answer.

I cant set 2 variables to - each other and equal a third variable in swift

== is for checking if two values are equal. = is for assigning a new value to a variable.

Just like when you define a new variable, the destination is on the left and the new value is on the right.

var warmup: Int = 60

In your case, you'll want the following.

nexx = count - warmup


Related Topics



Leave a reply



Submit