195
180

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[Swift]String、Int、Doubleの型変換をそれぞれまとめてみた。

Last updated at Posted at 2015-01-05

俺の名はケンヤ。難事件をいくつも迷宮入りさせたニート。しかし ある時謎の組織に入社し薬を飲まされ身体が縮んで Braian になっちゃった。

##Int → String

var intNum: Int = 37
var str: String = String(intNum)
//"78"

##Int → Double

var intNum: Int = 37
var doubleNum: Double = Double(intNum)
//37.0

##Double → String

var doubleNum: Double = 1.234
var str: String = String("\(doubleNum)")
//"1.234"

##Double → Int

var doubleNum: Double = 1.234
var intNum: Int = Int(doubleNum)
//1

##String → Double

import = Darwin
var str: String = "10.5"
var doubleNum: Double = atof(str)
//10.5

##String → Int

var str: String = "37"
var intNum: Int = str.toInt()!
// 37

String型からDouble型への変換では"Darwin"を使用したのですが他にいい方法ってあるのでしょうか??

まだまだ勉強が足りませんが、不備やアドバイスなどのコメントを頂けますと幸いです!!!

195
180
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
195
180

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?