0

I created a small currency converter. By cons I do not know how to give it a format with thousand separator. for example, the result is 10 000 000. 10 I would like to thank you if you can advise me. Below is my code

class ChangeViewController: UIViewController {

@IBOutlet weak var usdamount: UITextField!

@IBOutlet weak var label: UILabel!



var noImput = "Merci de saisir une valeur"

var currencynum = Int()


override func viewDidLoad() {
    super.viewDidLoad()

    label.text = ""

    // Do any additional setup after loading the view.

}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}
// rejet du clavier
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
    view.endEditing(true)
    super.touchesBegan(touches, with: event)
}

@IBAction func currency(_ sender: UISegmentedControl) {


    switch sender.selectedSegmentIndex{
    case 0:
        currencynum = 0
        break
    case 1:
        currencynum = 1
        break
    default:
        break
    }
}

@IBAction func convert(_ sender: AnyObject) {

    if usdamount.text == "" {
        label.text = noImput

    } else {
        let num = Int(usdamount.text!)
    switch currencynum {

    case 0:
        let convertednum = Double(num!) * 14500
        label.text = "\(num!) Euro(s) = \(convertednum) Rupiah(s)"
        break
    case 1:
        let convertednum = Double(num!) * 0.000067
        label.text = "\(num!) Rupiah = \(convertednum) Euros"
        break
    default:
        break
    }
            }
}


}
SWOON
  • 19
  • 7
  • 2
    Possible duplicate of [Struggling with NSNumberFormatter in Swift for currency](http://stackoverflow.com/questions/24960621/struggling-with-nsnumberformatter-in-swift-for-currency) – Droppy Oct 11 '16 at 08:10

1 Answers1

1

Hello swift already has a currency UI style, for your case

SWIFT 3

 let amount = Int(usdamount.text!)
 let numberFormatter = NumberFormatter()
 numberFormatter.currencyCode = "USD"   
 numberFormatter.numberStyle = NumberFormatter.Style.currency 
 let newValue = numberFormatter.string(from: NSNumber(value: amount!))
 print(newValue)

SWIFT 2

 let amount = Int(usdamount.text!)
 let numberFormatter = NSNumberFormatter()
 numberFormatter.currencyCode = "EUR"
 numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
 let newValue  = numberFormatter.stringFromNumber(amount)!
 print(newValue)

enter image description here

Özgür Ersil
  • 6,909
  • 3
  • 19
  • 29
  • Thank you for your reply, I do not know if I place the code correctly, but in the résulat I still do not have separation – SWOON Oct 11 '16 at 08:49
  • thank you, I'll do the tests because in the playground actually it works, but after adding the code in the résulat remains unchanged. I do something wrong or forget something. I'm going to test and find my error – SWOON Oct 11 '16 at 09:32
  • check if you are using swift2 or 3 – Özgür Ersil Oct 11 '16 at 10:37
  • Apple Swift version 3.0 (swiftlang-800.0.46.2 clang-800.0.38) Target: x86_64-apple-macosx10.9 – SWOON Oct 11 '16 at 13:25