1

In my Core Data app I save 3 Floating point numbers from 3 UITexFields. To do that I had to convert them to Strings.

Now the problem is that the values are rounded to .0.

How do I do it?

nyTankning.liter = (textFieldLiter.text as NSString).floatValue
nyTankning.kronor = (textFieldKronor.text as NSString).floatValue
nyTankning.literpris = (textFieldLiterpris.text as NSString).floatValue 
jww
  • 97,681
  • 90
  • 411
  • 885
Måns Sandberg
  • 72
  • 1
  • 10
  • Can you explain what the issue is, I didn't get that. If you don't want the ., convert it to int – Midhun MP Jan 13 '15 at 21:23
  • When the value is saved e.g. if I type in 3,14 it is rounded to 3.0. I still want it to be 2 decimals – Måns Sandberg Jan 13 '15 at 21:32
  • possible duplicate of [Precision String Format Specifier In Swift](http://stackoverflow.com/questions/24051314/precision-string-format-specifier-in-swift) – David Berry Jan 13 '15 at 23:00
  • @David the main issue here is the decimal separator as you can see in the comment right above yours – Leo Dabus Jan 14 '15 at 01:13
  • Is the problem converting floating point numbers in european format? Ie., using comma instead of period as the decimal separator? If that's true, make sure you have your locale set properly and use NSNumberFormatter. – David Berry Jan 14 '15 at 15:30
  • If that is indeed the problem, you can use `localizedStringWithFormat` instead of `stringWithFormat` (on OS-X, it's not available on iOS for some reason) – David Berry Jan 14 '15 at 15:34

2 Answers2

2

Your problem there is that you are trying to use a comma "," instead of a period "."

struct Number {
    static let formatter = NumberFormatter()
}
extension String {
    var converted: String? {
        return fractionDigits()
    }
    var doubleValue: Double? {
        return Number.formatter.number(from: self)?.doubleValue
    }
    var floatValue: Float? {
        return Number.formatter.number(from: self)?.floatValue
    }
    func fractionDigits(min: Int = 2, max: Int = 2) -> String? {
        Number.formatter.decimalSeparator = "."
        if let result = Number.formatter.number(from: self) {
            Number.formatter.minimumFractionDigits = min
            Number.formatter.maximumFractionDigits = max
            return Number.formatter.string(from: result)
        } else {
            Number.formatter.decimalSeparator = ","
            if let result = Number.formatter.number(from: self) {
                Number.formatter.minimumFractionDigits = min
                Number.formatter.maximumFractionDigits = max
                return Number.formatter.string(from: result)
            }
        }
        return nil
    }
}

"1,1222".converted              // "1.12"
"2".converted                   // "2.00"

"1,1222".converted?.doubleValue  // 1.12
"2".converted?.doubleValue       // 2.0

"1,1222".converted?.floatValue   // 1.12000000476837
"2".converted?.floatValue        // 2.0

"1.1222".converted              // "1.12"
"1.1222".converted?.doubleValue  // 1.12
"1.254".converted?.floatValue    // 1.25

"1.254".fractionDigits(min: 1, max: 2) // "1.25"
"1.2".fractionDigits(min: 1, max: 2)   // "1.2"
Leo Dabus
  • 229,809
  • 59
  • 489
  • 571
0

You can use the NSString(format: "%.02f", nyTanking.liter)

Jeremiah Jessel
  • 430
  • 3
  • 13