In the C language which,To convert type does not seem to how difficult,The learning Swift when there is confusion。
Which of them seem to remember spoken tutorial,But remember,This has found,On record:
1 2 3 4 |
假定三个变量: var i:Int = 5 var f:Float = 3.3 var d:Double = 4.455 |
To convert to Double Int or Float ,It is as follows
1 2 |
d = Double(i) f = Float(i) |
To convert Int,It is as follows:
1 2 |
i = Int(d) i = Int(f) |
To convert to String,It is as follows:
1 2 3 4 |
var string:String string = "\(i)" string = "\(d)" string = "\(f)" |
Int To String Switch, etc.,It is as follows
1 2 3 4 5 6 7 8 9 10 11 |
var s:String = “2.555” var t:String = “3” //t可以直接使用 //t.toInt直接转换为Int型 //而s 用这个方法的话输出会是nil var s1 = s as NSString //有这一句之后,就可以用下方的方法转换为int、float、double s1.integerValue s1.doubleValue s1.floatValue |
There is an extension method can retain double digits,You want to keep the number of bits can be manipulated
1 2 3 4 5 6 7 |
extension Double { func format(f:String) ->String { return NSString(format:"%\(f)f",self) } } |
d.format(“.2”)Two digits after the decimal point is reserved.,In this case the output will become "4.46"
Original article written by LogStudio:R0uter's Blog » Swift Int Float Double String type convert
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1014.html