2017-03-01 79 views
-1

我知道如何打印值串聯它們在字符串中,例如鑄造值:如何連接在迅速

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)") 

謝謝,

+1

'INT(雙)'是*不*鑄造 - 這是一個*轉換*。 – Hamish

回答

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)") 
2

內部\()任何表達式,所以打印在一個字符串轉換爲一個IntDouble的值...

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

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

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