1

I am trying to convert the value of a text field, which the user enters. As users are able to enter decimal values in european number format too, I use numberformatter for that. This was what I was trying:

let newprice = MaximumPriceLabel.text as! String
print(newprice) -> result: Optional("10,00")
print(formatter.number(from: newprice as String)) -> result: nil
print(Double(formatter.number(from: ("10,11"))!)) -> result: 10.11 -> that is what I want

So there is a value stored in the variable newprice, but when formatting, it returns nil. When I test it with a manual value, it works. Is there anything I am doing wrong? Formatter.local is set to Locale.current.

Update: The problem seems to be that the MaximumPriceLabel.text contains not only the value I need, but really the text "Optional("10,00") - which fails when converting to a number. The value is filled like this:

 self.MaximumPriceLabel.text = String(describing: formatter.string(from: NSNumber(value: desprice)))

and when I do a print afterwards, I receive "Optional("Optional(\"10,00\")")" -> even when I clear the variable MaximumPriceLabel.text first.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Alexander Ko
  • 195
  • 18
  • 1
    Unrelated but why do you cast the `text` property which is defined as `String(?)` to `String` and later once again to `String`? – vadian Oct 22 '17 at 20:32
  • Based on your comments below my answer, you need to update your question with the code that sets the `text` property of your `MaximumPriceLabel` label. It seems your label actually contains the text `Optional(...)` and of course such text isn't a valid number. So your solution is to fix the code that sets the label so the label's text only contains the number itself and not the `Optional(...)` part. – rmaddy Oct 23 '17 at 15:32
  • And what is this `desprice` variable? How is it declared/initialized? – rmaddy Oct 23 '17 at 15:44
  • When I print desprice, I get 10.0. desprice is filled by a function with a return value of type double. let desprice = getDesiredPrice(checkString: myVariables.ASIN) – Alexander Ko Oct 23 '17 at 15:48
  • That fixed it! Thanks a lot for your kind help, to all of you guys. – Alexander Ko Oct 23 '17 at 15:57

1 Answers1

1

You need to learn how to deal with optionals.

Your first issue is how you set the label's text. Please note that NumberFormatter string(from:) returns an optional String. And never, never use String(describing:) to display information to a user. That should only be used for debugging.

Start by changing:

self.MaximumPriceLabel.text = String(describing: formatter.string(from: NSNumber(value: desprice)))

to:

if let text = formatter.string(from: NSNumber(value: desprice)) {
    MaximumPriceLabel.text = text
}

That will fix the issue of the label's text having the text Optional(...).

Then your code for converting the label's text back to a string needs to be updated as:

if let newprice = MaximumPriceLabel.text {
    if let price = formatter.number(from: newprice) {
        print("The price is \(price)")
    } else {
        print("Invalid number: \(newprice)")
    }
}
rmaddy
  • 314,917
  • 42
  • 532
  • 579