プリミティブ型 – 型変換 早見表
型の異なるプリミティブ型のキャスト方法、簡易早見表です。
Int
Int → String
let int : Int = 23 let string : String = String(int) print(string) // 23
Int → Double
let int : Int = 23 let double: Double = Double(int) print(double) // 23.0
Int → Float
let int : Int = 23 let float: Float = Float(int) print(float) // 23.0
Float
Float → String
let float : Float = 23.5 let string : String = String(float) print(string) // 23.5
Float → Int (小数点以下切り捨て)
let float : Float = 23.5 let int : Int = Int(float) print(int) // 23
Float → Double
let float : Float = 23.5 let double : Double = Double(float) print(double) // 23.5
Double
Double → String
let double: Double = 23.5 let string : String = String(double) print(string) // 23.5
Double → Int (小数点以下切り捨て)
let double : Double = 23.5 let int : Int = Int(double) print(int) // 23
Double → Float
let double : Double = 23.5 let float : Float = Float(double) print(float) // 23.5
Bool
Bool → String
let bool : Bool = true let string : String = String(bool) print(string) // true
Bool → Int
let bool : Bool = true let int : Int = Int(bool) print(int) // 1
Bool → Float
let bool : Bool = true let float : Float = Float(bool) print(float) // 1.0
Bool → Double
let bool : Bool = true let double : Double = Double(bool) print(double) // 1.0
おすすめの本