我有一个字符串: “我有67 + 89.06美元。”
现在我想进行计算并将字符串更新为: “我有156.06美元。”
使用Swift如何执行此任务?
原始字符串中的数字可以是任何值,而不仅仅是我上面写的内容。
答案 0 :(得分:0)
我建议您阅读developer.apple.com上的Apple文档
"I have 67+89.06 Dollars."
// In order to get a Float value both operands need to be of type Float.
let valueCalculate = "I have \(Float(67) + Float((89.06))) Dollars"
print(valueCalculate)
// Convert String to Int
var stringNumber = "123456"
var convertToInt = Int(stringNumber)
// Convert Float value to Int
var stringNumber = "123456"
var convertToInt = Int(stringNumber)
//convert value of type Float written as a String to Float
var stringWithFloatValue = "123.250"
var stringtoFloatValue = Float(stringWithFloatValue)
//convert Float value to Int
if stringtoFloatValue != nil {
let floatToInt = Int(stringtoFloatValue!)
}