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

7

15

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 for all the available App Store currencies like GBP. So I think it's better to roll-back to string representation of currency.

The method used should be absolutely OK for any currency that App Store supports.

Averir answered 24/2, 2011 at 13:22 Comment(0)
R
37

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]!

Rabjohn answered 24/2, 2011 at 13:43 Comment(7)
I know that, but I want to do it automatically, so $ -> USD, £-> GBP and so on. Can I do it or it's also prohibited?Averir
The code does exactly this. It replaces the currency symbol with the 3 letter currency code.Rabjohn
This could be the best exposition on the NSNumberFormatter that I've read. Thanks! I am especially grateful for your inclusion of setPositiveFormat. I was looking for setFormat and couldn't find it, I had almost given up on this until now :)Mailable
You can use [currencyFormatter setCurrencyCode:@"EUR"]; and the € sign will appear.Runagate
Where does the "¤" symbol come from? Where is it documented? Also, you defined NSString *currencyString twice...Ticknor
I don't think the ¤ is documented anywhere. I found it by looking at the various formats the numberFormatter creates.Rabjohn
'¤' is the generic currency symbol. When ascii was designed, there wasnt enough space to include all currency symbols, so only few made it in: Dollar $, Yen ¥, Pounds £. The engineers felt like they should include a symbol representing "currency". They decided to use a golden coin (can u see it shining?)Barracuda
A
9

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!

Assembled answered 28/2, 2012 at 11:39 Comment(0)
A
4

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
Antony answered 19/9, 2014 at 19:35 Comment(0)
F
3

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)!
    }
}
Flume answered 24/3, 2015 at 22:5 Comment(1)
Really great job.Villalobos
T
2

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.

Thermotaxis answered 24/2, 2011 at 13:37 Comment(0)
F
1

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.

Fog answered 24/2, 2011 at 13:27 Comment(0)
I
0

SKProduct price is a NSDecimalNumber object, so one way you could do this would be to extend the NSDecimalNumber class. Here's my Swift code for this:

extension NSDecimalNumber {
    func asCurrency(locale:NSLocale) -> String? {
        var numberFormatter = NSNumberFormatter()
        numberFormatter.formatterBehavior = NSNumberFormatterBehavior.Behavior10_4
        numberFormatter.numberStyle = NSNumberFormatterStyle.CurrencyStyle
        numberFormatter.locale = locale
        return numberFormatter.stringFromNumber(self)
    }
}

in Objective-C it would look like this:

@interface NSDecimalNumber (CurrencyExtension)

- (NSString*) asCurrency: (NSLocale *)locale;

@end

@implementation NSDecimalNumber (DellExtensions)

- (NSString*) asCurrency: (NSLocale *)locale {
    NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
    [numberFormatter setFormatterBehavior:NSNumberFormatterBehavior10_4];
    [numberFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
    [numberFormatter setLocale:product.priceLocale];
    return [numberFormatter stringFromNumber:product.price];
}

@end
Imprecise answered 16/3, 2015 at 13:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.