5

I must be missing something obvious but since I lost way too much time on this, I'll let the experts have a look. I'm just willing to get a string formatted exactly how I want it but the currency symbol is giving me an issue : it's using some kind of international code when I want only the symbol.

NSNumberFormatter *formatter = [[[NSNumberFormatter alloc] init] autorelease];
[formatter setNumberStyle:NSNumberFormatterDecimalStyle];
[formatter setCurrencyCode:currencyCode];
[formatter setPositiveFormat:@"#,##0.00 ¤"];
[formatter setNegativeFormat:@"-#,##0.00 ¤"];

This results in : $US 1,250.56 instead of $ 1,250.56

I won't set the locale since I just have a symbol : EUR / USD / GBP etc..

omartin
  • 116
  • 1
  • 5
  • To repro this, many English speakers will have to set the formatter's region. For example, using `[formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"sv_SE"]]`, the formatter formats $1234.99 as "1 234,99 US$". – Jeremy W. Sherman Oct 27 '11 at 19:46
  • My savior ! If I indeed override the locale to EN, I can do pretty much what I want :) – omartin Oct 28 '11 at 11:22

2 Answers2

4

If you want a dollar sign and just a dollar sign, why not just specify a format like '$#,##0.00' with a literal $? Alternatively, specify a locale that will format the amount the way you want by default.

Specifying NSNumberFormatterCurrencyStyle is just a shortcut to setting whatever the locale's standard currency format is, which will include whether the symbol is prefixed or suffixed to the amount, but has little bearing on what you're doing, as you are specifying the format by hand.

You're seeing US$ because it is formatting the currency USD appropriately for your region. (Though you must have mixed up your format string and your output, because it does respect whether you request prefix or suffix placement of the currency code.) But darvids0n is mostly right:

  • One currency symbol in the format string places the locale's symbol for that currency, which is "$" in en and US$ in many others;
  • Two places the international symbol, like "USD";
  • Three places the locale's display name for that currency, like "US dollar".

Here is an example of this behavior using the Swedish locale (inherited by default, not set explicitly; that's what my machine is set in right now):

> nf := NSNumberFormatter alloc init
> nf setNumberStyle:NSNumberFormatterDecimalStyle
> amt := 1250.56

> nf setPositiveFormat:'¤ #,##0.00'
> nf stringFromNumber:amt
'US$ 1 250,56'

> nf setPositiveFormat:'#,##0.00 ¤'
> nf stringFromNumber:amt
'1 250,56 US$'

> nf setPositiveFormat:'#,##0.00 ¤¤'
> nf stringFromNumber:amt
'1 250,56 USD'

> nf setPositiveFormat:'#,##0.00 ¤¤¤'
> nf stringFromNumber:amt
'1 250,56 US-dollar'

(The syntax is that of F-Script.)

Jeremy W. Sherman
  • 35,901
  • 5
  • 77
  • 111
1

Try NSNumberFormatterCurrencyStyle.

This simple code:

NSNumberFormatter *currencyFormatter = [[[NSNumberFormatter alloc] init] autorelease];
[currencyFormatter setNumberStyle:NSNumberFormatterCurrencyStyle];
[currencyFormatter setCurrencyCode:currencyCode];

NSString *formatted = [currencyFormatter stringFromNumber:[NSDecimalNumber decimalNumberWithString:@"1250.56"]];
NSLog(@"%@", formatted);

Outputs:

2011-10-26 19:18:49.141 Craplet[8716:707] $1,250.56

That said, I can't repro your issue. If I use decimal style, with your positive and negative explicit formatting works:

// ...
[currencyFormatter setNumberStyle:NSNumberFormatterDecimalStyle]; 
// ...
[currencyFormatter setPositiveFormat:@"#,##0.00 ¤"];
[currencyFormatter setNegativeFormat:@"-#,##0.00 ¤"];

2011-10-26 19:29:28.943 Craplet[9458:707] 1,250.56 $
2011-10-26 19:29:28.949 Craplet[9458:707] -1,250.56 $
bryanmac
  • 38,941
  • 11
  • 91
  • 99