0

For some reason, I can't convert the Price string to double. When I do it always returns nil.

       func calculateAirfare(checkedBags: Int, distance: Int, travelers: Int) {


        let bagsPrices = Double(checkedBags * 25)
        let mileCosts = Double(distance) * 0.10
        let price = (bagsPrices + mileCosts) * Double(travelers)

        /// Format price

        let currencyFormatter = NumberFormatter()
        currencyFormatter.numberStyle = .currency

        let priceString = currencyFormatter.string(from: NSNumber(value: price))

         print(priceString) -> "Optional("$750.00")"
        if let double = Double(priceString) {
            print(double) -> nil

        }
    }
John
  • 799
  • 6
  • 22

2 Answers2

0

You can use your same formatter to go back to a number like so:

let number = currencyFormatter.number(from: priceString)

and get the doubleValue like:

let numberDouble = number.doubleValue
DrewG23
  • 427
  • 3
  • 11
  • Right. A double will never be able to have the dollar sign. Only the string will be able to. Where you have `currencyFormatter.string(from: NSNumber(value: price))` is what adds that dollar sign. So you display this to the user, and utilize either the number or `doubleValue` like shown above if you need to calculate things or send information to the server. – DrewG23 Jan 16 '20 at 13:34
-1

The price is already double from the line

let price = (bagsPrices + mileCosts) * Double(travelers)

thus no need to convert it to double. The code below will return a string with a $ symbol

currencyFormatter.string(from: NSNumber(value: price))

To get a double from that string then you need to remove the $ symbol

which you can do using removeFirst()

priceString?.removeFirst()

After that, the string can be converted to Double. The complete code is:

func calculateAirfare(checkedBags: Int, distance: Int, travelers: Int) {


    let bagsPrices = Double(checkedBags * 25)
    let mileCosts = Double(distance) * 0.10
    let price = (bagsPrices + mileCosts) * Double(travelers)

    /// Format price

    let currencyFormatter = NumberFormatter()
    currencyFormatter.numberStyle = .currency

    var priceString = currencyFormatter.string(for: price)
    priceString?.removeFirst()
    print(priceString!)

    if let double = Double(priceString!) {
        print(double)
    }
}
byaruhaf
  • 4,128
  • 2
  • 32
  • 50
  • 1
    No need to create a NSNumber object. You can use Formatter's method `string(for: Any)` in your case `currencyFormatter.string(for: price)` – Leo Dabus Jan 16 '20 at 01:24
  • Okay, but how do I put the money sign back? – John Jan 16 '20 at 13:36
  • @John you want a double or a string? – byaruhaf Jan 16 '20 at 15:18
  • I want a double but with the money, is that possible? – John Jan 16 '20 at 15:20
  • That not possible, the $ symbol can only be used with strings. you can add the $ symbol to a double and create a string – byaruhaf Jan 16 '20 at 15:22
  • Do all your calculations as double but if you want to add the $ symbol then it must be a string. – byaruhaf Jan 16 '20 at 15:25
  • have you checkout https://stackoverflow.com/questions/29782982/how-to-input-currency-format-on-a-text-field-from-right-to-left-using-swift/29783546#29783546 – byaruhaf Jan 16 '20 at 15:37
  • @byaruhaf can you help me with this problem? I'm willing to pay for it! https://stackoverflow.com/questions/59699361/handling-3d-interaction-and-ui-controls-in-augmented-reality – John Jan 18 '20 at 16:55
  • @John will take a look, no payment needed. – byaruhaf Jan 19 '20 at 02:46