Search code examples
iosswift

How to convert Int to Double in Swift playground?


I made a new Playground and wrote the code as following.

var value = 33 //This will make an integer type of value variable with value 33

value = 22.44 //This doesn't execute as its assigning Double/Float value to an integer.

I tried

1. 
value = Double(value)
value = 44.55 //Error here

2. 
value = Double(33.44) //Error here too.

3.
value : Double = 33.22 //Error here too.

Now what should I do to assign floating point to value.

NOTE: I am at learning level in Swift.

Thanks.


Solution

  • Declaring var value = 33 will infer the type of value as Int. If you want to assign 33 and make value as Float or Double, you have to declare the type var value : Double = 33 or convert 33 to a Double while assign var value = Double(33).