如何在swift中连接一个转换值

时间:2017-03-01 10:33:17

标签: swift string casting int concatenation

我知道如何打印在字符串中连接它们的值,例如:

var double:Double = 25.43

var double2:Double = 68.78

print("the sum of \(double) + \(double2) is equal to \(double + double2)")

我也知道如何投射值,以便我可以添加不同类型的值,如下所示:

var int:Int = 3

var double:Double = 25.43

print(int + Int(double))

但是如何在其中打印一个具有强制转换值的句子?

var int:Int = 3

var double:Double = 25.43

这样的事情不起作用:

print("the sum of \(int) + \Int(double) is equal to \(double + double2)")

谢谢,

2 个答案:

答案 0 :(得分:3)

您只缺少一组括号:

var int:Int = 3

var double:Double = 25.43
var double2:Double = 68.78

print("the sum of \(int) + \(Int(double)) is equal to \(double + double2)")

答案 1 :(得分:2)

评估\()内的任何表达式,以便在字符串中打印转换为Double的{​​{1}}的值...

Int

所以在你的例子中......

print("\(Int(double))")