9

Please could you help me here? I need to understand how to convert a String into an Int, Float or Double! This problem occurs when I'm trying to get the value from an UITextField and need this type of conversion!

I used to do it like this:

var myValue : Float = myTextField.text.bridgeToObjectiveC().floatValue

but since Xcode 6 beta 6 it doesn't seem to work anymore!

I've tried also like this:

var str = "3.14"

// Conversion from StringValue to an Int
var intValue : Int = str.toInt()!

// Other converstion from StringValue to an Int
var intOtherValue : Int = Int(str)

// Converstion from StringValue to a Float
var floatValue : Float = str.bridgeToObjectiveC().floatValue

// Converstion from StringValue to a Double
var doubleValue : Double = Double(str)

Please help me or tell me where I can find the answer! Many thanks!

Charles Caldwell
  • 16,649
  • 4
  • 40
  • 47
365Cases
  • 93
  • 1
  • 1
  • 3

5 Answers5

27

Convert String to NSString and Use convenience methods:

var str = "3.1"

To Int

var intValue : Int = NSString(string: str).integerValue // 3

To Float

var floatValue : Float = NSString(string: str).floatValue // 3.09999990463257

To Double

var doubleValue : Double = NSString(string: str).doubleValue // 3.1


Reference

var doubleValue: Double { get }
var floatValue: Float { get }
var intValue: Int32 { get }
@availability(OSX, introduced=10.5)
var integerValue: Int { get }
@availability(OSX, introduced=10.5)
var longLongValue: Int64 { get }
@availability(OSX, introduced=10.5)
Maxim Shoustin
  • 77,483
  • 27
  • 203
  • 225
  • 2
    You can cast the string to `NSString` instead of using an `NSString` constructor: `var floatValue = (str as NSString).floatValue`. – vacawama Sep 07 '14 at 11:36
4

Use:

Int(string:String)
Double(string:String)
Float(string:String)

Which return an optional which is nil if it fails to parse the string. For example:

var num = 0.0
if let unwrappedNum = Double("5.0") {
    num = unwrappedNum
} else {
    print("Error converting to Double")
}

Of course you can force unwrap if you are sure:

var foo = Double("5.0")!

Extending String

If you are doing this in more than a few places, and want error handling to be handled the same everywhere then you may want to extend String with conversion methods:

For example:

extension String {
    func toDouble() -> Double {
        if let unwrappedNum = Double(self) {
            return unwrappedNum
        } else {
            // Handle a bad number
            print("Error converting \"" + self + "\" to Double")
            return 0.0
        }
    }
}

and then to use it:

let str = "4.9"
var num = str.toDouble()
floatingpoint
  • 570
  • 5
  • 8
1
public extension String {
    public func toFloat() -> Float? {
        return Float.init(self)
    }

    public func toDouble() -> Double? {
        return Double.init(self)
    }
}
Tinvy
  • 11
  • 2
0
var holdTextFieldToStringValue = myTextField.text

//convert from string to Int

var holdIntValue = holdTextFieldToStringValue.toInt()!

//convert from string to Double

var holdDoubleValue = Double((holdTextFieldToStringValue as NSString).doubleValue)
Will Tate
  • 33,439
  • 9
  • 77
  • 71
Malik
  • 921
  • 11
  • 12
0
let strValue = "14.03"

let result = (strValue as NSString).floatValue
HebeleHododo
  • 3,620
  • 1
  • 29
  • 38
Ranjan
  • 95
  • 9