Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable locale for NSNumberFormatter

Im using an NSNumberFormatter with the format of "##,##0.00", but my locale is set to the region of South Africa which uses a currency format like "## ##0.0", is there a way for me to disable NSNumberFormatter from using a locale, and to use specifically what i've typed in for the format? i've tried just going:

formatter.locale = nil;

and

formatter.formatterBehavior = NSNumberFormatterNoStyle;

some output from my program:

format = #,##0.00

result = -8 933 434,38

there is a variable

formatter.localizesFormat = NO;

but that is only for OS X

A server is going to tell me what number format to use, and needs to override what the user has set their region to.

relevant code:

self.amountFormat = @"##,##0.00";

NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
NSLog(@"self.amountFormat = %@", amountFormat);
[formatter setNumberStyle:NSNumberFormatterNoStyle];
[formatter setPositiveFormat: self.amountFormat];
[formatter setLenient:YES];

NSNumber* newNumber = [NSNumber numberWithDouble: [number doubleValue] / 100.0];

NSString* numberString = [formatter stringFromNumber: newNumber];

NSLog(@"numberString = %@",numberString);

output

[Currency.m:64] self.amountFormat = ##,##0.00
[Currency.m:73] numberString = 1 533 434,34

change your settings under general - international - region format to South Africa if you wish to test

like image 681
Fonix Avatar asked Aug 15 '14 05:08

Fonix


1 Answers

As for formatting string, , means grouping separator and . means decimal separator which specified by the locale, NOT literal character. See the format specification.
To override that, you must specify the locale that grouping separator is , and decimal separator is . like en_US_POSIX

[formatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"]];

OR manually specify grouping separator and decimal separator like:

[formatter setGroupingSeparator:@","];
[formatter setDecimalSeparator:@"."];
like image 155
rintaro Avatar answered Oct 08 '22 16:10

rintaro