Warpling
Warpling

Reputation: 2095

Displaying price in cents

I'm trying to display a price in cents (99¢) instead of dollars ($0.99) using NSNumberFormatter and a locale.

This code turns a price of 99 cents into the string: $0.99. Is there a way to get NSNumberFormatter to try to use smaller/cent denominations, when possible, instead?

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

Upvotes: 1

Views: 1113

Answers (1)

sv_lane
sv_lane

Reputation: 3089

I'm pretty sure there is no easy and general-purpose way to do this. I think you just have to handle it on a case-by-case basis. If you want to localize your app for an international audience, but expect many of your customers to be from the US, you can do something like what I did:

if priceString == "$0.99" && NSLocale.currentLocale().objectForKey(NSLocaleCountryCode) as? String == "US" {
    priceString = "99¢"
}

Upvotes: 1

Related Questions