I need Something like
var a = 1
var b = 2
var c = a + b should be 12 (Int) instead of 3.
var a = 12
var b = 34
var c = a + b should be 1234 (Int) instead of 46.
I am not figuring out how can we do this for any number?
One way is to convert two both Int to String, concatenate and covert it again that String to Int, but I don't think it's efficient.
Thank you in advance if you have a solution.
12+34 = 12*10^2 + 34 = 1200+34 = 1234
func logC(val: Double, forBase base: Double) -> Double {
return log(val)/log(base)
}
var a = 10
var b = 0
let x = b == 10 ? 2 : b == 0 ? 1 : ceil(logC(val: Double(b), forBase: 10))
var c = Int(Double(a) * pow(10, x) + Double(b))
print(c)
You can do it with simple conversion like below:
var a = 1
var b = 2
var c = Int("\(a)\(b)") // Result 12
var a1 = 12
var b1 = 34
var c1 = Int("\(a1)\(b1)") // Result 1234
var a2 = 122344
var b2 = 9022
var c2 = Int("\(a2)\(b2)") // Result 1223449022
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With