Binary operator '+' cannot be applied to operands of type 'Int' and 'Double'

Just use the Double() function to initialize a Double value with the integer value.
import UIKit

let myInt : Int = 111
let myDouble : Double = 222.222

//Incorrect addition of Integer and Double
print(String(format: "%.9f", myInt + myDouble))

//Correct Way to add Integer and Double
print(String(format: "%.9f", Double(myInt) + myDouble))
//Result = 333.222000000

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.