gpt4 book ai didi

ios - 货币数字的格式

转载 作者:行者123 更新时间:2023-11-29 00:38:49 31 4
gpt4 key购买 nike

我创建了一个小型货币转换器。缺点我不知道如何给它一个带有千位分隔符的格式。例如,结果是 10 000 000。 10 如果您能给我建议,我将非常感谢。下面是我的代码

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
}
}
}


}

最佳答案

Hello swift 已经有适合您情况的货币 UI 样式

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

关于ios - 货币数字的格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39972890/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com