Is there a simple way to format currency into string in iOS?

前端 未结 7 1087

I need a way to format the price from NSNumber into a string like this: \"USD 0.99\", not \"$ 0.99\".

My game uses custom fonts, and they could not have the symbols

7条回答
  •  情歌与酒
    2020-12-23 15:55

    The Swift Example:

    var currencyFormatter = NSNumberFormatter()
    currencyFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
    currencyFormatter.locale = priceLocale //SKProduct->priceLocale
    var currencyString = currencyFormatter.internationalCurrencySymbol
    var format = currencyFormatter.positiveFormat
    format = format.stringByReplacingOccurrencesOfString("¤", withString: currencyString)
    currencyFormatter.positiveFormat = format
    
    var formattedCurrency = currencyFormatter.stringFromNumber(price) //SKProduct->price
    
    println("formattedCurrency: \(formattedCurrency)")//formattedCurrency: 0,89 EUR
    

提交回复
热议问题