How to properly format currency on ios

后端 未结 7 1114
梦谈多话
梦谈多话 2020-12-01 09:19

I\'m looking for a way to format a string into currency without using the TextField hack.

For example, i\'d like to have the number \"521242\" converted into \"5,212

7条回答
  •  时光说笑
    2020-12-01 09:43

    func getCurrencyFormat(price:String)->String{
        let convertPrice = NSNumber(double: Double(price)!)
        let formatter = NSNumberFormatter()
        formatter.numberStyle = .CurrencyStyle
        formatter.currencyCode = "USD"        
    
        let convertedPrice = formatter.stringFromNumber(convertPrice)       
        return convertedPrice!
    }
    

    Note:- A currency code is a three-letter code that is, in most cases, composed of a country’s two-character Internet country code plus an extra character to denote the currency unit. For example, the currency code for the Australian dollar is “AUD”.

提交回复
热议问题