0

I am trying to convert a String into a Double value in Swift. After doing a lot of research, I believe that this would be the best method to turn my string to an Double. But now I am getting an error that I have no idea of how to fix.

var purchaseAmount: Double = 200

var cashTendered: Double = 425.45

var changeRequired: Double = 0

var currencyValues = ["100", "50", "20", "10", "5", "2", "1", "0.50", "0.20", "0.10", "0.05"]

import Cocoa

if cashTendered < purchaseAmount {
    println("Not enough cash tendered")
} else {
    changeRequired = cashTendered - purchaseAmount
    for currency in currencyValues {
        var doubleCurrency = (currency as NSString).doubleValue
        var amountOfCurrency = changeRequired / doubleCurrency
        amountOfCurrency = floor(amountOfCurrency)
        changeRequired = changeRequired - (amountOfCurrency * currency)
        changeRequired = round(changeRequired * 100)/100
        println("$\(currency) X \(Int(amountOfCurrency))")
    }
}

Here is the error: enter image description here

What do I have to do to solve this problem?

Kampai
  • 22,848
  • 21
  • 95
  • 95
Sachin
  • 737
  • 1
  • 9
  • 23
  • Images usually help, but when it's about code is always better to use old plain text format – Antonio Feb 18 '15 at 07:19
  • @Antonio I added the code in text – Sachin Feb 18 '15 at 07:24
  • The code and the picture don't match. What's the actual error you're seeing now? – David Berry Feb 18 '15 at 07:32
  • possible duplicate of [Swift - How to convert String to Double](http://stackoverflow.com/questions/24031621/swift-how-to-convert-string-to-double) – Kai Mattern Feb 18 '15 at 07:32
  • @David sorry, I edited my question to match the picture. – Sachin Feb 18 '15 at 07:35
  • 1
    Please everyone add a comment when you downvote a question, especially with new users. It's common courtesy. – Tancrede Chazallet Feb 18 '15 at 07:41
  • I'm also curious about why you have an array of strings to convert to,doubles instead of just having an array of doubles that you don't have to convert. – David Berry Feb 18 '15 at 07:42
  • @David This is to help me with the output that I have to achieve. I need to print an output which is a whole number for some of the currencies, and a double value for the others. I figured that this would be the most efficient way of completing my task. – Sachin Feb 18 '15 at 07:47
  • 1
    that is another good example why you should not ignore the explicit type-definition. – holex Dec 18 '15 at 11:03

2 Answers2

2

First of all initialise String array like below:

let currencyValues : [String] = ["100", "50", "20", "10", "5", "2", "1", "0.50", "0.20", "0.10", "0.05"]

However it is not required type declaration because of Swift has vital feature Type Interface. But most of the practice it is good to define type while working with array.

And about converting string to double you can use below code:

for currency : String in currencyValues {
   let doubleCurrency = NSNumberFormatter().numberFromString(currency)!.doubleValue
   print(doubleCurrency)
}
Kampai
  • 22,848
  • 21
  • 95
  • 95
0

Posting code would be easier to look at than an image, but if all else fails there is NSNumberFormatter. Other advantage of NSNumberFormatter would allow you to convert both directions and get nice output for things like currency.

Jeremy Pope
  • 3,342
  • 1
  • 16
  • 17
  • For this task, I only need to convert the string to a Double value so that I can do some calculations. – Sachin Feb 18 '15 at 07:11