3

Is it possible to convert fractions that are entered into UITextField into decimals?

So far, this is what I have tried. If I enter in 2/3 into my UITextField, I get 2.0 as my answer.

    @IBOutlet weak var answer: UITextField!

    var sAnswer = (answer.text as NSString).doubleValue

    println(sAnswer)

If I just use this, I will get 2/3 but it is a string and not a decimal answer of 0.6666666666...

    @IBOutlet weak var answer: UITextField!

    var sAnswer = answer.text as NSString

    println(sAnswer)
Eugene
  • 239
  • 2
  • 16
  • You'll have to detect that it is written as faction and convert it your self. The NSNumberFormatter class could be of help. – Tobias May 07 '15 at 01:39
  • I see, so for example, to split the string to its individual parts, use if-else to detect the divide sign (/) and then divide it manually? – Eugene May 07 '15 at 01:47
  • 1
    Yep. You might have to validate the input before hand so you don't get unexpected things. Like PI/360 or (5/6)/2. – Tobias May 07 '15 at 01:50

4 Answers4

3

You need to parse and then calculate the fraction by yourself in a method or extension. One way of doing is as follows:

extension UITextField {

    var fraction: Float {
        var comps = text.componentsSeparatedByString("/")

        //Do some validation here so as to check the correct format of fraction text.

        var op1 = NSString(string: comps[0]).floatValue
        var op2 = NSString(string: comps[1]).floatValue

        return op1/op2
    }
}
Abdullah
  • 7,143
  • 6
  • 25
  • 41
1

No. The text will always be a string. But what you could do is, make an extension for your UITextField where you split your string at the / and divide it. Then you return the double-value. Like that:

extension UITextField {
    var xo: Double! {
        get {
            var value = self.text.componentsSeparatedByString("/")
            return (value[0] as NSString).doubleValue / (value[1] as NSString).doubleValue
        }
    }
}

Then you use it like that:

answer.doubleValue
Christian
  • 22,585
  • 9
  • 80
  • 106
  • I tried this but the doubleValue returns a 2.0 if I enter in a 2/3 (0.6666...) in the UITextField – Eugene May 07 '15 at 01:11
  • 1
    @Eugene I've edited my answer. You could just separate your string at the / and divide the first with the second number. – Christian May 07 '15 at 01:52
0

A more modern Swift 5 solution:

func parse(fractionString: String) -> Float? {
    guard fractionString.range(of: "^\\d+\\/\\d+$", options: .regularExpression) != nil else { return nil }

    let components = fractionString.split(separator: "/")
    if let numerator = Float(components[0]),
        let denominator = Float(components[1]),
        denominator != 0 {

        return numerator / denominator
    }
    
    return nil
}

The regex check at the beginning is just making sure that the input string is just digits with a single "/" in the middle.

Ryan H
  • 1,656
  • 14
  • 15
0

SWIFT 5.3

var fraction: String = "1/3"

var new = fraction.components(separatedBy: "/")

print(new)

["1", "3"]

From this point you can return the divided result. I provided this answer because the "separated by"notation was changed in a newer version of swift.


func convertStringToFraction(stringNumber: String) -> Float {

let stringFloat = stringNumber.components(separatedBy: "/")
let numerator = Float(stringFloat[0])!
let denominator = Float(stringFloat[1])!

return numerator / denominator

}