[英]Float to Int conversion Swift
我想將浮點值轉換為int。 例如,我的浮點值是40.22,我需要像這樣的int值4022。
let price : String = valueArray.objectAtIndex(5) as! String
let floatValue : Int = Int(price)! * 100
您可以這樣嘗試:
var myInt = Int(40.22*100)
要么
var myInt = Int(price*100)
這是最好的解決方案,
let price : Float = 40.22
let stringValue : String = "\(price)"
print(stringValue) //This prints "40.22"
let intString = stringValue.stringByReplacingOccurrencesOfString(".", withString: "")
print(intString) //This prints "4022"
if let intValue = Int(intString) {
print(intValue) //This gives 4022
}
您可以嘗試此解決方案,我希望一切順利。
let price : String = "40.4534522"
let myStringArr = price.componentsSeparatedByString(".")
let finalString = myStringArr[0] + myStringArr[1]
let requiredInt = Int(finalString)
print(requiredInt!)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.