Note that there are some explanatory texts on larger screens.

plurals
  1. POBug with iPhone Currency for Euro format with NSDecimalNumber using NSLocale?
    text
    copied!<p>My app uses monetary values, and I would like to represent these values in the currency and formats that the user has set on their iPhone however, when testing, I seem to be having troubles with the German EURO format. </p> <p>I have a method that returns a decimal number (for storing in core data) from the currency formatted string. The string was actually formatted by another (similar) method using NSlocale. </p> <p>I have tested with UK, US, Japanese and German locales, so far only Germany has caused any problems. </p> <p>Here's the code, the string being passed is for example: 2,56 €</p> <pre><code>+ (NSDecimalNumber*)TLCurrencyFormatFromString:(NSString*)strNumber { NSNumberFormatter *fmtrCurrencyFromStr = nil; fmtrCurrencyFromStr = [[NSNumberFormatter alloc] init]; [fmtrCurrencyFromStr setFormatterBehavior:NSNumberFormatterBehavior10_4]; [fmtrCurrencyFromStr setLocale:[NSLocale currentLocale]]; [fmtrCurrencyFromStr setNumberStyle:NSNumberFormatterCurrencyStyle]; [fmtrCurrencyFromStr setGeneratesDecimalNumbers:YES]; NSLog(@"Currency Separator: %@", [fmtrCurrencyFromStr decimalSeparator]); //prints a comma NSLog(@"Currency Code: %@", [fmtrCurrencyFromStr currencyCode]); //prints EUR int currencyScale = [fmtrCurrencyFromStr maximumFractionDigits]; NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:currencyScale raiseOnExactness:FALSE raiseOnOverflow:TRUE raiseOnUnderflow:TRUE raiseOnDivideByZero:TRUE]; NSDecimalNumber* currencyNumber = [[NSDecimalNumber alloc] initWithDecimal:[[fmtrCurrencyFromStr numberFromString:strNumber] decimalValue]]; NSLog(@"Currency Number = %@", [currencyNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior]); return [currencyNumber decimalNumberByRoundingAccordingToBehavior:roundingBehavior]; </code></pre> <p>}</p> <p>The 'currencyNumber' variable gets printed as: -18446912453607470532000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000</p> <p>whereas in all the other locales, it would print 2.56 for storing into Core Data as NSDecimalNumber. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload