Fonix
Fonix

Reputation: 11597

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

Upvotes: 7

Views: 2220

Answers (3)

Jignesh B
Jignesh B

Reputation: 508

-(NSString *)amount:(NSString *)val{
    NSNumber *someNumber = [NSNumber numberWithDouble:[val doubleValue]];
    NSNumberFormatter *nf = [[NSNumberFormatter alloc] init];
    [nf setNumberStyle:NSNumberFormatterCurrencyStyle];
    // if you want to add the  custom currency than you need to specify otherwise it will take the $ USD currency. here you have to pass the string with valid string amount without ex: 135.21 (Valid), 13,2000.00 (Invalid), $123 (Invalid).
    return  [nf stringFromNumber:someNumber];
}

Upvotes: 0

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 40008

As rintaro answer suggest you have to add grouping separator and decimal separator

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
[formatter setNumberStyle:NSNumberFormatterNoStyle];
[formatter setPositiveFormat:@"#,##0.00"];
[formatter setGroupingSeparator:@","]; //This line taken from above answer
[formatter setDecimalSeparator:@"."]; //This line taken from above answer
[formatter setLenient:YES];
NSString *str = [formatter stringFromNumber:@123414231412.45];
NSLog(@"%@",str);

Console output

2014-08-15 11:28:59.075 TestApp[700:60b] 123,414,231,412.45

Upvotes: 0

rintaro
rintaro

Reputation: 51911

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:@"."];

Upvotes: 11

Related Questions