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

前端 未结 7 1083

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:48

    If you want it localized (ie the currency on the correct side of the price) it is a bit of a hassle.

    NSDecimalNumber *price = [NSDecimalNumber decimalNumberWithString:@"1.99"];
    NSLocale *priceLocale = [[[NSLocale alloc] initWithLocaleIdentifier:@"de_DE"] autorelease]; // get the locale from your SKProduct
    
    NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    [currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [currencyFormatter setLocale:priceLocale];
    NSString *currencyString = [currencyFormatter internationalCurrencySymbol]; // EUR, GBP, USD...
    NSString *format = [currencyFormatter positiveFormat];
    format = [format stringByReplacingOccurrencesOfString:@"¤" withString:currencyString];
        // ¤ is a placeholder for the currency symbol
    [currencyFormatter setPositiveFormat:format];
    
    NSString *formattedCurrency = [currencyFormatter stringFromNumber:price];
    

    You have to use the locale you get from the SKProduct. Don't use [NSLocale currentLocale]!

    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-23 15:55

    The easiest way to achieve this would be to use the NSNumberFormatter class to format the NSNumber value as required.

    Whilst the methods are too numerous to mention, this provides a wide variety of output formatting capabilities including the setInternationalCurrencySymbol: method that should be of particular interest.

    0 讨论(0)
  • 2020-12-23 15:57

    Nice example I found here http://bendodson.com/weblog/2014/12/10/skproduct-localized-price-in-swift/

    import StoreKit
    
    extension SKProduct {
    
        @objc func localizedPrice() -> String {
            let formatter = NSNumberFormatter()
            formatter.numberStyle = .CurrencyStyle
            formatter.locale = self.priceLocale
            return formatter.stringFromNumber(self.price)!
        }
    }
    
    0 讨论(0)
  • 2020-12-23 15:59

    use formatter in this way or you can also customize it

    NSNumberFormatter *numberFormatter = [[[NSNumberFormatter alloc] init] autorelease];
    [numberFormatter setNumberStyle: NSNumberFormatterCurrencyStyle];
    

    or like this

    [formatter setFormat:@"USD ###.00"];
    

    i think you can check the currency for the country and store that in string and give that to the formatter.

    0 讨论(0)
  • 2020-12-23 16:00

    The – productsRequest:didReceiveResponse: method gives you back a list of SKProducts.

    Each product contains a property priceLocale which contains the local currency of the product for the current user.

    You could use the following sample code (apple's) to format it:

    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setLocale:product.priceLocale];
    NSString *formattedString = [numberFormatter stringFromNumber:product.price];
    

    Good luck!

    0 讨论(0)
提交回复
热议问题